在 MySQL 中 SELECT * WHERE var == [one of many alternatives]?

mysqlmysqli database

使用 IN() 进行 select * where var== [one of many alternatives]。让我们首先创建一个表 −

mysql> create table DemoTable1624
    -> (
    -> ClientId int,
    -> ClientName varchar(20)
    -> );
Query OK, 0 rows affected (0.39 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable1624 values(101,'Chris Brown');
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable1624 values(102,'David Miller');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1624 values(103,'John Smith');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1624 values(104,'Carol Taylor');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1624 values(105,'Adam Smith');
Query OK, 1 row affected (0.19 sec)

使用 select 语句显示表中的所有记录 −

mysql> select * from DemoTable1624;

这将产生以下输出 −

+----------+--------------+
| ClientId | ClientName   |
+----------+--------------+
|      101 | Chris Brown  |
|      102 | David Miller |
|      103 | John Smith   |
|      104 | Carol Taylor |
|      105 | Adam Smith   |
+----------+--------------+
5 rows in set (0.00 sec)

这是实现 SELECT * WHERE var == [众多替代方案之一] 的查询 −

mysql> select * from DemoTable1624 where ClientId in(101,102,104);

这将产生以下输出 −

+----------+--------------+
| ClientId | ClientName   |
+----------+--------------+
|      101 | Chris Brown  |
|      102 | David Miller |
|      104 | Carol Taylor |
+----------+--------------+
3 rows in set (0.00 sec)

相关文章