如何在MySQL Insert语句中添加where子句?

您需要为此使用UPDATE语句。

语法如下

update yourTableName
set yourColumnName1=yourValue1,yourColumnName2=yourValue2,....N
where yourCondition;

让我们为我们的示例创建一个表

create table addWhereClauseDemo
   -> (
   -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentName varchar(30),
   -> StudentPassword varchar(40)
   -> );

使用insert命令在表中插入一些记录。

查询如下

insert into addWhereClauseDemo(StudentName,StudentPassword) values('John','John123456');
insert into addWhereClauseDemo(StudentName,StudentPassword) values('Carol','99999');
insert into addWhereClauseDemo(StudentName,StudentPassword) values('Bob','OO7Bob');
insert into addWhereClauseDemo(StudentName,StudentPassword) values('David','David321');

使用select语句显示表中的所有记录。

查询如下

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子句的查询,即更新记录

update addWhereClauseDemo
-> set StudentName='Maxwell',StudentPassword='Maxwell44444' where StudentId=4;
Rows matched: 1 Changed: 1 Warnings: 0

让我们再次检查表记录。

查询如下

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)