MySQL 中,当列等于 'x, y, z' 时如何查询?
您可以使用 IN 运算符。
语法如下:
SELECT *FROM yourTableName WHERE yourColumnName IN(‘yourValue1’,‘yourValue2’,‘yourValue3’,...........N);
为了理解上述语法,让我们创建一个表。创建表的查询如下:
mysql> create table InOperatorDemo -> ( -> ClientId int -> ); Query OK, 0 rows affected (0.52 sec)
使用 insert 命令在表中插入一些记录。查询如下:
mysql> insert into InOperatorDemo values(101); Query OK, 1 row affected (0.19 sec) mysql> insert into InOperatorDemo values(110); Query OK, 1 row affected (0.11 sec) mysql> insert into InOperatorDemo values(120); Query OK, 1 row affected (0.17 sec) mysql> insert into InOperatorDemo values(230); Query OK, 1 row affected (0.11 sec) mysql> insert into InOperatorDemo values(270); Query OK, 1 row affected (0.15 sec) mysql> insert into InOperatorDemo values(300); Query OK, 1 row affected (0.14 sec)
使用 select 语句显示表中的所有记录。查询如下:
mysql> select *from InOperatorDemo;
以下是输出:
+----------+ | ClientId | +----------+ | 101 | | 110 | | 120 | | 230 | | 270 | | 300 | +----------+ 6 rows in set (0.00 sec)
以下是使用 IN 运算符从列中获取值的查询:
mysql> select *from InOperatorDemo where ClientId IN('120','270','300');
以下是输出:
+----------+ | ClientId | +----------+ | 120 | | 270 | | 300 | +----------+ 3 rows in set (0.00 sec)
广告