Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a table with the columns: (this is only an example I have 50K records)

Name,   Number

Joe     Null
Michael Null
Moses   Null

I to update the number with a sequence number from 1-3 so it will look like this:

Name,   Number

Joe     1
Michael 2
Moses   3

How can I do it in SQL for Mysql in one SQL command

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
271 views
Welcome To Ask or Share your Answers For Others

1 Answer

SET @rank:=0;
update T
set Number=@rank:=@rank+1;

UPDATE

alternative way with one statement

UPDATE T
JOIN (SELECT @rank := 0) r
SET Number=@rank:=@rank+1;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...