在 MySQL 查詢中匹配陣列的元素


我們首先建立一個表 table −

mysql> create table DemoTable1523
   -> (
   -> Id int,
   -> Value int
   -> );
Query OK, 0 rows affected (0.76 sec)

使用 insert 命令在表中插入一些記錄 −

mysql> insert into DemoTable1523 values(1,56);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1523 values(2,78);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1523 values(1,34);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1523 values(2,45);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1523 values(1,99);
Query OK, 1 row affected (0.10 sec)

使用 select 語句從表中顯示所有記錄 −

mysql> select * from DemoTable1523;

這將產生以下輸出 −

+------+-------+
| Id   | Value |
+------+-------+
|    1 |    56 |
|    2 |    78 |
|    1 |    34 |
|    2 |    45 |
|    1 |    99 |
+------+-------+
5 rows in set (0.00 sec)

以下是匹配陣列元素的查詢。在這裡,我們使用匹配值 34、99 和 56 選中 id −

mysql> select Id from DemoTable1523
   -> where Value IN ('34','99','56')
   -> group by Id
   -> having count(*)=3;

這將產生以下輸出 −

+------+
| Id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

更新於: 11-Dec-2019

2K+ 瀏覽量

開啟你的 職業

完成課程以獲得認證

入門
廣告
© . All rights reserved.