- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL update a column with an int based on order?
The syntax is as follows to update a column with an int based on order
set @yourVariableName=0; update yourTableName set yourColumnName=(@yourVariableName:=@yourVariableName+1) order by yourColumnName ASC;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table updateColumnDemo -> ( -> Id int, -> OrderCountryName varchar(100), -> OrderAmount int -> ); Query OK, 0 rows affected (1.76 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into updateColumnDemo(Id,OrderCountryName) values(10,'US'); Query OK, 1 row affected (0.46 sec) mysql> insert into updateColumnDemo(Id,OrderCountryName) values(20,'UK'); Query OK, 1 row affected (0.98 sec) mysql> insert into updateColumnDemo(Id,OrderCountryName) values(30,'AUS'); Query OK, 1 row affected (0.77 sec) mysql> insert into updateColumnDemo(Id,OrderCountryName) values(40,'France'); Query OK, 1 row affected (1.58 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from updateColumnDemo;
The following is the output
+------+------------------+-------------+ | Id | OrderCountryName | OrderAmount | +------+------------------+-------------+ | 10 | US | NULL | | 20 | UK | NULL | | 30 | AUS | NULL | | 40 | France | NULL | +------+------------------+-------------+ 4 rows in set (1.00 sec)
Here is the query to update a column with an int based on order
mysql> set @sequenceNumber=0; Query OK, 0 rows affected (0.00 sec) mysql> update updateColumnDemo -> set OrderAmount=(@sequenceNumber:=@sequenceNumber+1) -> order by OrderAmount ASC; Query OK, 4 rows affected (0.25 sec) Rows matched: 4 Changed: 4 Warnings: 0
Let us check the table records once again.
The query is as follows
mysql> select *from updateColumnDemo;
The following is the output
+------+------------------+-------------+ | Id | OrderCountryName | OrderAmount | +------+------------------+-------------+ | 10 | US | 1 | | 20 | UK | 2 | | 30 | AUS | 3 | | 40 | France | 4 | +------+------------------+-------------+ 4 rows in set (0.00 sec)
- Related Articles
- Update a column based on another MySQL table’s column
- Update MySQL column based on email address?
- Update a table based on StudentId value in MySQL?
- MySQL index on column of int type?
- Update a MySQL column with JSON format?
- MySQL query to update different fields based on a condition?
- ORDER BY records in MySQL based on a condition
- SUM a column based on a condition in MySQL
- Update a column of text with MySQL REPLACE()
- How to change the order of a matrix in increasing order based on a single column?
- Performing a MySQL LIKE comparison on an INT field?
- Update only the int in MySQL Field
- How do I remove ON UPDATE CURRENT_TIMESTAMP from an existing column in MySQL?
- How to update MySQL column with random value?
- How to add a new column in an R data frame with count based on factor column?

Advertisements