如何在 MySQL 中搜索逗号分隔值表?

mysqlmysqli database更新于 2023/10/16 8:29:00

要在逗号分隔值表中搜索,请使用 LIKE 运算符。让我们首先创建一个表 −

mysql> create table DemoTable675(Value text);
Query OK, 0 rows affected (0.55 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable675 values('10,56,49484,93993,211,4594');
Query OK, 1 row affected (0.28 sec)
mysql> insert into DemoTable675 values('4,7,1,10,90,23');
Query OK, 1 row affected (0.41 sec)
mysql> insert into DemoTable675 values('90,854,56,89,10');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable675 values('11,22,344,67,89');
Query OK, 1 row affected (0.10 sec)

使用 select 语句显示表中的所有记录 −

mysql> 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)

以下是在逗号分隔值的表中搜索的查询 −

mysql> 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)

相关文章