MySQL:使用 Group By 更新字段?
要使用 GROUP BY 更新字段,请用 ORDER BY LIMIT 与 UPDATE 命令一起使用 −
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)
使用插入命令在表中插入某些记录 −
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)
使用 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; Query OK, 1 row affected (0.13 sec) 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)
广告