How can I change the value of an instance of a row in MySQL table?


UPDATE command along with WHERE clause can be used to change the value of an instance of a row. Basically, MySQL will change the value on the basis of the condition given in the query. Following example can demonstrate it

Suppose we want to change the name from ‘Ram’ to ‘Mohit’ in the ‘testing’ table given below −

mysql> Select * from testing;

+----+---------+
| Id | Name    |
+----+---------+
| 1  | Harshit |
| 2  | Lovkesh |
| 3  | Ram     |
| 4  | Gaurav  |
+----+---------+

4 rows in set (0.00 sec)

Now by running the following query we can change the instance of row to ‘Mohit’ where it is ‘ram’.

mysql> UPDATE TESTING SET Name = 'MOHIT' WHERE name = ‘ram’;
Query OK, 1 row affected (0.13 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> Select * from testing;

+-----+---------+
| id1 | Name    |
+-----+---------+
| 1   | Harshit |
| 2   | Lovkesh |
| 3   | MOHIT   |
| 4   | Gaurav  |
+-----+---------+

4 rows in set (0.00 sec)

Updated on: 29-Jan-2020

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements