如何在 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)
广告