MySQL 使用重复的ID更新表
以下是语法-
update yourTableName set yourColumnName1= yourValue where yourColumnName2=yourValue order by yourColumnName2 DESC LIMIT 1;
让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Id int, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.61 sec)
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(1,'John'); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable values(2,'Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(2,'Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(2,'David'); Query OK, 1 row affected (0.18 sec)
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+------+--------+ | Id | Name | +------+--------+ | 1 | John | | 2 | Chris | | 2 | Robert | | 2 | David | +------+--------+ 4 rows in set (0.00 sec)
以下是查询以更新SQLWHERE子句中的多个字段的表-
mysql> update DemoTable set Name='Adam Smith' where Id=2 order by Id DESC LIMIT 1; Query OK, 1 row affected (0.28 sec) Rows matched: 1 Changed: 1 Warnings: 0
让我们再次检查表记录-
mysql> select *from DemoTable;
输出结果
+------+------------+ | Id | Name | +------+------------+ | 1 | John | | 2 | Adam Smith | | 2 | Robert | | 2 | David | +------+------------+ 4 rows in set (0.00 sec)