在 MySQL 中使用逻辑 AND 运算符更新
为此,你可以在 WHERE 子句中使用 AND 运算符。我们首先创建一个表 −
mysql> create table DemoTable1616 -> ( -> StudentId int, -> StudentName varchar(20), -> StudentMarks int -> ); Query OK, 0 rows affected (0.44 sec)
使用 insert 命令向表中插入一些记录 −
mysql> insert into DemoTable1616 values(101,'Chris',56); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1616 values(102,'Bob',87); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1616 values(103,'David',56); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1616 values(101,'Bob',89); Query OK, 1 row affected (0.14 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1616;
这将产生以下输出 −
+-----------+-------------+--------------+ | StudentId | StudentName | StudentMarks | +-----------+-------------+--------------+ | 101 | Chris | 56 | | 102 | Bob | 87 | | 103 | David | 56 | | 101 | Bob | 89 | +-----------+-------------+--------------+ 4 rows in set (0.00 sec)
以下是使用逻辑 AND 运算符更新的查询 −
mysql> update DemoTable1616 set StudentName='Adam' where StudentId=101 and StudentMarks=56; Query OK, 1 row affected (0.48 sec) Rows matched: 1 Changed: 1 Warnings: 0
让我们再次检查表记录 −
mysql> select * from DemoTable1616;
这将产生以下输出 −
+-----------+-------------+--------------+ | StudentId | StudentName | StudentMarks | +-----------+-------------+--------------+ | 101 | Adam | 56 | | 102 | Bob | 87 | | 103 | David | 56 | | 101 | Bob | 89 | +-----------+-------------+--------------+ 4 rows in set (0.00 sec)
广告