使用 AND 和 OR 操作符使用单次 MySQL 查询获取特定记录
首先创建一个表 −
mysql> create table DemoTable2015 -> ( -> StudentId int, -> StudentName varchar(20), -> StudentCountryName varchar(20) -> ); Query OK, 0 rows affected (1.20 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable2015 values(1,'Chris','US'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable2015 values(2,'Bob','UK'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2015 values(3,'David','AUS'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2015 values(4,'Robert','US'); Query OK, 1 row affected (0.16 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable2015;
这将生成以下输出 −
+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ | 1 | Chris | US | | 2 | Bob | UK | | 3 | David | AUS | | 4 | Robert | US | +-----------+-------------+--------------------+ 4 rows in set (0.00 sec)
以下是一个查询,可通过单个查询获取特定记录 −
mysql> select *from DemoTable2015 -> where StudentName='David' OR StudentCountryName='UK' and StudentId=3;
这将生成以下输出 −
+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ | 3 | David | AUS | +-----------+-------------+--------------------+ 1 row in set (0.00 sec)
广告