要使用GROUP BY更新字段,请在UPDATE命令中使用ORDER BY LIMIT-
mysql> create table DemoTable2018 -> ( -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> EmployeeName varchar(20), -> EmployeeSalary int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable2018(EmployeeName,EmployeeSalary) values('Chris',10000); mysql> insert into DemoTable2018(EmployeeName,EmployeeSalary) values('David',12560); mysql> insert into DemoTable2018(EmployeeName,EmployeeSalary) values('Chris',25400);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable2018;
这将产生以下输出-
+------------+--------------+----------------+ | EmployeeId | EmployeeName | EmployeeSalary | +------------+--------------+----------------+ | 1 | Chris | 10000 | | 2 | David | 12560 | | 3 | Chris | 25400 | +------------+--------------+----------------+ 3 rows in set (0.00 sec)
这是对MySQL更新字段的查询-
mysql> update DemoTable2018 -> set EmployeeSalary=EmployeeSalary+2500 where EmployeeName='Chris' -> order by EmployeeId limit 1; Rows matched: 1 Changed: 1 Warnings: 0
让我们再次检查表记录-
mysql> select *from DemoTable2018;
这将产生以下输出-
+------------+--------------+----------------+ | EmployeeId | EmployeeName | EmployeeSalary | +------------+--------------+----------------+ | 1 | Chris | 12500 | | 2 | David | 12560 | | 3 | Mike | 25400 | +------------+--------------+----------------+ 3 rows in set (0.00 sec)