MySQL 如何在 WHERE 子句中实现 OR 运算符?
当任何一个操作数为真时,OR 运算符给出真结果。现在让我们看一个示例并创建一个表 −
mysql> create table DemoTable663(ClientId int,ClientName varchar(100),ClientAge int); Query OK, 0 rows affected (0.55 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable663 values(100,'Chris',45); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable663 values(101,'Robert',29); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable663 values(102,'John',45); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable663 values(103,'Chris',35); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable663 values(104,'Sam',45); Query OK, 1 row affected (0.72 sec)
使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable663;
这将生成以下输出 −
+----------+------------+-----------+ | ClientId | ClientName | ClientAge | +----------+------------+-----------+ | 100 | Chris | 45 | | 101 | Robert | 29 | | 102 | John | 45 | | 103 | Chris | 35 | | 104 | Sam | 45 | +----------+------------+-----------+ 5 rows in set (0.00 sec)
以下是实现 OR 运算符的查询 −
mysql> select *from DemoTable663 where ClientName='Chris' OR ClientAge=45;
这将生成以下输出 −
+----------+------------+-----------+ | ClientId | ClientName | ClientAge | +----------+------------+-----------+ | 100 | Chris | 45 | | 102 | John | 45 | | 103 | Chris | 35 | | 104 | Sam | 45 | +----------+------------+-----------+ 4 rows in set (0.00 sec)
广告