MySQL的SELECT喜欢和喜欢

问题描述

我在名为papers的表中有一个名为products的列,其值如下:

“ 1,2,3,4,5,6”,而在另一行中,其格式为“ 3,5”

我想用PHP启动此查询

SELECT * from products where papers like "%5%" AND papers like "%3%" and papers like "%4%"

TL; DR

我想选择一个包含多个字符串的列,而忽略顺序

解决方法

在MySQL中,您可以使用find_in_set()

select * 
from products 
where find_in_set('3',papers) and find_in_set('4',papers) and find_in_set('5',papers)

请注意,您的问题表明数据库设计有缺陷。您不应该在单个字符串列中存储多个整数值。相反,您应该将每个整数值放在单独的行中(可能在另一个表中),该行将通过外键约束引用主表。推荐阅读:Is storing a delimited list in a database column really that bad?