MySQL 如何在Insert语句中添加where子句
您需要使用UPDATE语句。
语法如下:
update yourTableName
set yourColumnName1=yourValue1,yourColumnName2=yourValue2,....N
where yourCondition;
让我们创建一个示例表
mysql> create table addWhereClauseDemo
-> (
-> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> StudentName varchar(30),
-> StudentPassword varchar(40)
-> );
Query OK, 0 rows affected (0.45 sec)
使用insert命令在表中插入一些记录。
查询如下:
mysql> insert into addWhereClauseDemo(StudentName,StudentPassword) values('John','John123456');
Query OK, 1 row affected (0.14 sec)
mysql> insert into addWhereClauseDemo(StudentName,StudentPassword) values('Carol','99999');
Query OK, 1 row affected (0.24 sec)
mysql> insert into addWhereClauseDemo(StudentName,StudentPassword) values('Bob','OO7Bob');
Query OK, 1 row affected (0.16 sec)
mysql> insert into addWhereClauseDemo(StudentName,StudentPassword) values('David','David321');
Query OK, 1 row affected (0.26 sec)
使用select语句从表中显示所有记录。
查询如下:
mysql> select *from addWhereClauseDemo;
输出如下:
+-----------+-------------+-----------------+
| StudentId | StudentName | StudentPassword |
+-----------+-------------+-----------------+
| 1 | John | John123456 |
| 2 | Carol | 99999 |
| 3 | Bob | OO7Bob |
| 4 | David | David321 |
+-----------+-------------+-----------------+
4 rows in set (0.00 sec)
这是添加where子句即更新记录的查询
mysql> update addWhereClauseDemo
-> set StudentName='Maxwell',StudentPassword='Maxwell44444' where StudentId=4;
Query OK, 1 row affected (0.18 sec)
Rows matched: 1 Changed: 1 Warnings: 0
让我们再次检查表记录。
查询如下:
mysql> select *from addWhereClauseDemo;
输出如下:
+-----------+-------------+-----------------+
| StudentId | StudentName | StudentPassword |
+-----------+-------------+-----------------+
| 1 | John | John123456 |
| 2 | Carol | 99999 |
| 3 | Bob | OO7Bob |
| 4 | Maxwell | Maxwell44444 |
+-----------+-------------+-----------------+
4 rows in set (0.00 sec)
阅读更多:MySQL 教程