MySQL 資料庫查詢,基於特定值從逗號分隔的值中獲取記錄
為此,您可以在 MySQL 中使用 REGEXP。假設您希望行記錄其中任何一個逗號分隔的值是 90。為此,請使用正則表示式。
我們首先建立一個表 −
mysql> create table DemoTable1447 -> ( -> Value varchar(100) -> ); Query OK, 0 rows affected (0.58 sec)
使用 insert 命令在表中插入一些記錄 −
mysql> insert into DemoTable1447 values('19,58,90,56'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1447 values('56,89,99,100'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1447 values('75,76,65,90'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1447 values('101,54,57,59'); Query OK, 1 row affected (0.14 sec)
使用 select 語句顯示錶中的所有記錄 −
mysql> select * from DemoTable1447;
這將生成以下輸出 −
+--------------+ | Value | +--------------+ | 19,58,90,56 | | 56,89,99,100 | | 75,76,65,90 | | 101,54,57,59 | +--------------+ 4 rows in set (0.00 sec)
以下是查詢語句,可基於特定值(此處為 90)從逗號分隔的值中獲取記錄 −
mysql> select * from DemoTable1447 where Value regexp '(^|,)90($|,)';
這將生成以下輸出 −
+-------------+ | Value | +-------------+ | 19,58,90,56 | | 75,76,65,90 | +-------------+ 2 rows in set (0.00 sec)
廣告