选择列值等于一个或等于二、三等的 MySQL 查询?
我们先创建一个表 -
mysql> create table DemoTable ( UserId int, UserName varchar(10), UserAge int ); Query OK, 0 rows affected (0.73 sec)
使用 insert 命令向表中插入记录 -
mysql> insert into DemoTable values(101,'Chris',23); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(102,'Robert',33); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(103,'David',25); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(104,'Carol',35); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(105,'Bob',29); Query OK, 1 row affected (0.17 sec)
使用 select 命令显示表中的记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+--------+----------+---------+ | UserId | UserName | UserAge | +--------+----------+---------+ | 101 | Chris | 23 | | 102 | Robert | 33 | | 103 | David | 25 | | 104 | Carol | 35 | | 105 | Bob | 29 | +--------+----------+---------+ 5 rows in set (0.00 sec)
以下是选择值等于一或等于二等(即我们表中具有不同年龄的 UserAge)的列的查询 -
mysql> select *from DemoTable where UserAge=25 or UserAge=35 or UserAge=33 or UserAge=29;
这将产生以下输出 -
+--------+----------+---------+ | UserId | UserName | UserAge | +--------+----------+---------+ | 102 | Robert | 33 | | 103 | David | 25 | | 104 | Carol | 35 | | 105 | Bob | 29 | +--------+----------+---------+ 4 rows in set (0.00 sec)
广告