To update field with GROUP BY, use ORDER BY LIMIT with UPDATE command −
mysql> create table DemoTable2018 -> ( -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> EmployeeName varchar(20), -> EmployeeSalary int -> ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2018(EmployeeName,EmployeeSalary) values('Chris',10000); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable2018(EmployeeName,EmployeeSalary) values('David',12560); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable2018(EmployeeName,EmployeeSalary) values('Chris',25400); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2018;
This will produce the following output −
+------------+--------------+----------------+ | EmployeeId | EmployeeName | EmployeeSalary | +------------+--------------+----------------+ | 1 | Chris | 10000 | | 2 | David | 12560 | | 3 | Chris | 25400 | +------------+--------------+----------------+ 3 rows in set (0.00 sec)
Here is the query to MySQL update field −
mysql> update DemoTable2018 -> set EmployeeSalary=EmployeeSalary+2500 where EmployeeName='Chris' -> order by EmployeeId limit 1; Query OK, 1 row affected (0.13 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable2018;
This will produce the following output −
+------------+--------------+----------------+ | EmployeeId | EmployeeName | EmployeeSalary | +------------+--------------+----------------+ | 1 | Chris | 12500 | | 2 | David | 12560 | | 3 | Mike | 25400 | +------------+--------------+----------------+ 3 rows in set (0.00 sec)