MySQL查询使用WHERE语句中的OR显示列及其值
让我们首先创建一个表-
mysql> create table DemoTable777 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(100), StudentAge int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable777(StudentName,StudentAge) values('Chris',23); mysql> insert into DemoTable777(StudentName,StudentAge) values('Robert',21); mysql> insert into DemoTable777(StudentName,StudentAge) values('Mike',19); mysql> insert into DemoTable777(StudentName,StudentAge) values('Bob',22);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable777;
这将产生以下输出-
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Chris | 23 | | 2 | Robert | 21 | | 3 | Mike | 19 | | 4 | Bob | 22 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)
以下是使用WHERE语句中的OR返回所有列的查询-
mysql> select *from DemoTable777 where StudentId IN(1,2) OR StudentName='Mike' OR StudentAge=22;
这将产生以下输出-
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Chris | 23 | | 2 | Robert | 21 | | 3 | Mike | 19 | | 4 | Bob | 22 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)