在 MySQL 中使用逻辑 AND 运算符进行更新
mysqlmysqli database
为此,您可以将 AND 运算符与 WHERE 子句结合使用。让我们首先创建一个表 −
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)