Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

updating columns with a sequence number mysql

Tags:

sql

sorting

mysql

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

like image 584
Dejell Avatar asked Jul 07 '11 20:07

Dejell


People also ask

How do I UPDATE a column value in MySQL?

MySQL UPDATE command can be used to update multiple columns by specifying a comma separated list of column_name = new_value. Where column_name is the name of the column to be updated and new_value is the new value with which the column will be updated.

How do you UPDATE columns with new values?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.

Can we use order by in UPDATE query in MySQL?

MySQL UPDATE UPDATE with ORDER BY and LIMITIf the ORDER BY clause is specified in your update SQL statement, the rows are updated in the order that is specified. If LIMIT clause is specified in your SQL statement, that places a limit on the number of rows that can be updated.


1 Answers

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; 
like image 90
triclosan Avatar answered Sep 29 '22 23:09

triclosan