MySQL 选择查询包含多个 WHERE?

mysqlmysqli database

要实现多个 WHERE,请使用 IN() IN MySQL。

以下是语法:

select *from yourTableName where yourColumnName IN(yourValue1,yourValue2,...N);

让我们首先创建一个表 -

mysql> create table DemoTable
   (
   Id int,
   Name varchar(100)
   );
Query OK, 0 rows affected (0.61 sec)

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

mysql> insert into DemoTable values(10,'John');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(59,'Carol');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(20,'Sam');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(45,'David');
Query OK, 1 row affected (0.73 sec)

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

mysql> select *from DemoTable;

输出

+------+-------+
| Id   | Name  |
+------+-------+
| 10   | John  |
| 59   | Carol |
| 20   | Sam   |
| 45   | David |
+------+-------+
4 rows in set (0.00 sec)

以下是实现多个 WHERE 的查询 -

mysql> select *from DemoTable where Id IN(59,45);

输出

+------+-------+
| Id   | Name  |
+------+-------+
| 59   | Carol |
| 45   | David |
+------+-------+
2 rows in set (0.14 sec)

相关文章