MySQL查询以使用AND OR进行选择
让我们首先创建一个表-
mysql> create table DemoTable ( StudentId int, StudentName varchar(20), StudentSubject varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(10,'Chris','MongoDB'); mysql> insert into DemoTable values(11,'David','MySQL'); mysql> insert into DemoTable values(12,'Bob','Java'); mysql> insert into DemoTable values(10,'Carol','MySQL'); mysql> insert into DemoTable values(10,'Chris','MongoDB'); mysql> insert into DemoTable values(14,'David','MySQL');
使用select语句显示表中的所有记录-
Mysql> select *from DemoTable;
这将产生以下输出-
+-----------+-------------+----------------+ | StudentId | StudentName | StudentSubject | +-----------+-------------+----------------+ | 10 | Chris | MongoDB | | 11 | David | MySQL | | 12 | Bob | Java | | 10 | Carol | MySQL | | 10 | Chris | MongoDB | | 14 | David | MySQL | +-----------+-------------+----------------+ 6 rows in set (0.00 sec)
以下是使用ANDOR执行选择的查询-
mysql> select *from DemoTable where StudentName='David' and StudentSubject='MySQL' OR StudentId=11;
这将产生以下输出-
+-----------+-------------+----------------+ | StudentId | StudentName | StudentSubject | +-----------+-------------+----------------+ | 11 | David | MySQL | | 14 | David | MySQL | +-----------+-------------+----------------+ 2 rows in set (0.00 sec)