如何從值列表中查詢特定的 MySQL varcahr id?


要從列表獲得特定 varchar ID,你可以使用 FIND_IN_SET()。我們首先建立一個表格 -

mysql> create table DemoTable
(
   Id varchar(255)
);
Query OK, 0 rows affected (0.51 sec)

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

mysql> insert into DemoTable values('10,100,1000');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values('101,120,2');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('3,4,5');
Query OK, 1 row affected (0.11 sec)

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

mysql> select *from DemoTable;

這將產生以下輸出 -

+-------------+
| Id          |
+-------------+
| 10,100,1000 |
| 101,120,2   |
| 3,4,5       |
+-------------+
3 rows in set (0.00 sec)

以下是在 MySQL 中查詢 id 的查詢 -

mysql> select *from DemoTable where find_in_set('2',Id);

這將產生以下輸出 -

+-----------+
| Id        |
+-----------+
| 101,120,2 |
+-----------+
1 row in set (0.03 sec)

更新於: 2019 年 9 月 25 日

207 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.