MySQL 查詢以在同一欄位中搜索逗號分隔值之間內容?


請使用 FIND_IN_SET。我們首先建立一個表 -

mysql> create table DemoTable
-> (
-> ListOfValues text
-> );
Query OK, 0 rows affected (0.56 sec)

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

mysql> insert into DemoTable values('10|20|30|40|50|60|100');
Query OK, 1 row affected (0.18 sec)

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

mysql> select *from DemoTable;

輸出

這將產生以下輸出 -

+-----------------------+
| ListOfValues          |
+-----------------------+
| 10|20|30|40|50|60|100 |
+-----------------------+
1 row in set (0.00 sec)

以下是查詢在一個欄位中搜索逗號分隔值之間的內容 -

mysql> select FIND_IN_SET(100, replace(ListOfValues,'|',',')) from DemoTable;

輸出

這將產生以下輸出 -

+-------------------------------------------------+
| FIND_IN_SET(100, replace(ListOfValues,'|',',')) |
+-------------------------------------------------+
| 7                                               |
+-------------------------------------------------+
1 row in set (0.04 sec)

更新時間: 30-6 月-2020

416 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.