如何在MySQL中用逗号分隔的值表中进行搜索?
要在用逗号分隔的值表中搜索,请使用LIKE运算符。让我们首先创建一个表-
create table DemoTable675(Value text);
使用插入命令在表中插入一些记录-
insert into DemoTable675 values('10,56,49484,93993,211,4594'); insert into DemoTable675 values('4,7,1,10,90,23'); insert into DemoTable675 values('90,854,56,89,10'); insert into DemoTable675 values('11,22,344,67,89');
使用select语句显示表中的所有记录-
select *from DemoTable675;
这将产生以下输出-
+----------------------------+ | Value | +----------------------------+ | 10,56,49484,93993,211,4594 | | 4,7,1,10,90,23 | | 90,854,56,89,10 | | 11,22,344,67,89 | +----------------------------+ 4 rows in set (0.00 sec)
以下是在逗号分隔值表中搜索的查询-
select *from DemoTable675 WHERE Value LIKE '10,%' OR Value LIKE '%,10' OR Value LIKE '%,10,%' OR Value= '10';
这将产生以下输出-
+----------------------------+ | Value | +----------------------------+ | 10,56,49484,93993,211,4594 | | 4,7,1,10,90,23 | | 90,854,56,89,10 | +----------------------------+ 3 rows in set (0.00 sec)