如何在 MySQL 中从值列表中查找特定的 varchar ID?

mysqlmysqli database更新于 2025/12/14 12:28:17

要从列表中获取特定的 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)

相关文章


有用资源