在 MySQL 中查詢包含由逗號分隔的多個 id 的特定行?


要查詢一行,請使用 FIND_IN_SET()。讓我們首先建立一個表 -

mysql> create table DemoTable
   -> (
   -> ListOfIds varchar(200)
   -> );
Query OK, 0 rows affected (0.72 sec)

使用 insert 命令向表中插入一些記錄 -

mysql> insert into DemoTable values('100,2093,678,686');
Query OK, 1 row affected (0.12 sec)

mysql> insert into DemoTable values('0595,9585,4885,95959');
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable values('0059954,95986884,9059596,9005');
Query OK, 1 row affected (0.20 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select *from DemoTable;

輸出

+-------------------------------+
| ListOfIds                     |
+-------------------------------+
| 100,2093,678,686              |
| 0595,9585,4885,95959          |
| 0059954,95986884,9059596,9005 |
+-------------------------------+
3 rows in set (0.00 sec)

以下是在 MySQL 中查詢包含由逗號分隔的多個 id 的特定行的查詢 -

mysql> select *from DemoTable where FIND_IN_SET('9005', ListOfIds);

輸出

+-------------------------------+
| ListOfIds                     |
+-------------------------------+
| 0059954,95986884,9059596,9005 |
+-------------------------------+
1 row in set (0.00 sec)

更新於: 30-07-2019

96 次瀏覽

開始您的 職業生涯

完成課程後獲得認證

開始
廣告