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)
广告