使用 AND OR 执行选择操作的 MySQL 查询
我们先创建一个表 −
mysql> create table DemoTable ( StudentId int, StudentName varchar(20), StudentSubject varchar(20) ); Query OK, 0 rows affected (0.62 sec)
使用 insert 命令向表中插入一些记录 −
mysql> insert into DemoTable values(10,'Chris','MongoDB'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(11,'David','MySQL'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(12,'Bob','Java'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(10,'Carol','MySQL'); Query OK, 1 row affected (0.51 sec) mysql> insert into DemoTable values(10,'Chris','MongoDB'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(14,'David','MySQL'); Query OK, 1 row affected (0.12 sec)
使用 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)
以下是使用 AND OR 执行选择操作的查询 −
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)
Advertisement