我有这个MySQL查询。MySQL喜欢多个值
我有这个内容
sports,shopping,pool,pc,games
shopping,pool,pc,games
sports,pub,swimming, pool, pc, games
为什么会出现这种类似的查询不起作用数据库字段? 我需要与运动或酒吧或两个领域?
SELECT * FROM table WHERE interests LIKE ('%sports%', '%pub%')
我有这个MySQL查询。MySQL喜欢多个值
我有这个内容
sports,shopping,pool,pc,games
shopping,pool,pc,games
sports,pub,swimming, pool, pc, games
为什么会出现这种类似的查询不起作用数据库字段? 我需要与运动或酒吧或两个领域?
SELECT * FROM table WHERE interests LIKE ('%sports%', '%pub%')
该(a,b,c)
列表仅适用于in
。对于like
,你必须使用or
:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
您的查询应该是SELECT * FROM `table` WHERE find_in_set(interests, "sports,pub")>0
我的理解是,你存储在你的表,这是一个误解的一个领域的利益。你应该明确地有一个“兴趣”表。这样做的
比较快的方式:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
是这样的:
WHERE interests REGEXP 'sports|pub'
发现这个解决方案在这里:http://forums.mysql.com/read.php?10,392332,392950#msg-392950
更多REGEXP这里:http://www.tutorialspoint.com/mysql/mysql-regexps.htm
唐“T忘了,如果你的AND
参数
筛选后,使用此功能使用括号:
WHERE id=123 and(interests LIKE '%sports%' OR interests LIKE '%pub%')
像@Alexis Dufrenoy建议,查询可能在manualSELECT * FROM `table` WHERE find_in_set('sports', interests)>0 OR find_in_set('pub', interests)>0
更多信息。
我认为它应该是'SELECT * FROM table WHERE find_in_set(interest,'sports,pub')',但是这种技术在大多数情况下可能会胜过正则表达式。 – 2017-09-14 04:02:06
你是对的。我编辑了我的答案。 – 2018-01-23 09:54:42