Update Command in Mysql

Update command is used to update record in the table based upon some condition, if condition is missing then all record will get updated.

Syntax : UPDATE tablename SET column1=value1,column2=value2 WHERE condition
Eg: UPDATE user SET name=’abhishek’,age=’22′ WHERE salary>22000

above query will update the name to ‘abhishek’ and age to ’22′ whose salary is greater than 22000.

Updating table based upon pattern matching.
Suppose we want to add 2 years to age whose name has ‘rao’ somewhere in the name(for eg Chandshakar raoh singh,Manisha rao)

Update user SET age=age+2 WHERE name LIKE ‘%rao%’

Some Tricky questions on Update Query

# Update user SET age=age+2 LIMIT 2,10

# Update user SET age=age+2 ORDER BY id LIMIT 2,10

Name id Col1 Col2
Row1 1 6 1
Row2 2 2 3
Row3 3 9 5
Row4 4 16 8
How to combine below Update query into one query

UPDATE table SET Col1 = 1 WHERE id = 1;
UPDATE table SET Col1 = 2 WHERE id = 2;
UPDATE table SET Col2 = 3 WHERE id = 3;
UPDATE table SET Col1 = 10 WHERE id = 4;
UPDATE table SET Col2 = 12 WHERE id = 4;

INSERT INTO table (id,Col1,Col2) VALUES (1,1,1),(2,2,3),(3,9,3),(4,10,12)

#select id, group_concat(subjects separator ‘, ‘)
from user_subjects group by id;