如何查询逗号分隔的字符串?

TABLE_A

column name: list_id
record 1: 1,2,5,6,8
record 2: 1,3,2
record 3: 6,7,2
record 4: 9,8,0

表-B

id ='2';

如何在逗号分隔的字符串中选择id='2'记录? 从上面的例子中,它应该返回记录1,2和3。

查询(请问如何修改此查询?):

SELECT * FROM Table_a,Table_b WHERE Table_b.id = Table_a.list_id;

使用find_in_set函数,但它不是最优化的方式,你应该规范你的数据。

SELECT * FROM 
Table_a AS a 
JOIN Table_b AS b ON FIND_IN_SET(b.id,a.list_id)

这应该适合你

SELECT * FROM Table_a,Table_b WHERE Table_b.id = Table_a.list_id and 
concat(',',Table_a.list_id,',') like '%,2,%'
链接地址: http://www.djcxy.com/p/32995.html

上一篇: How to query comma separated string?

下一篇: SQL Query to LINQ syntax using not exist and join