繁体   English   中英

查询SQL集

[英]Querying SQL Set

我有一个定义为set('nightlife', 'food', 'sports', 'culture', 'movies', 'general')的SQL字段set('nightlife', 'food', 'sports', 'culture', 'movies', 'general')

现在,我想运行一个查询,例如传递nightlife,food并希望结果包含类别包含夜生活或食物的所有记录。 因此,例如,关于nightlife,culture,sports的记录也应返回,因为其中包含夜生活。 我不确定该如何运行。 我尝试通过以下方式使用IN关键字:

'SELECT ... FROM table WHERE '$category' IN Categories但是这不起作用。

UPDATE

根据答案中的建议运行以下查询:

 SELECT * FROM images WHERE id = '2650225' AND WHERE FIND_IN_SET('sports', Categories ) >0 OR FIND_IN_SET('nightlife', Categories ) >0 ORDER BY delete_at ASC LIMIT 10 OFFSET 0 

收到以下错误:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE find_in_set('sports', Categories) > 0 or find_in_set('nightlife', Categori' at line 1

您可以将find_in_setor find_in_set使用:

select *
from yourtable
where find_in_set('nightlife', categories) > 0 or
      find_in_set('food', categories) > 0

根据您的编辑,您不能有多个where子句。 另外,您还需要在or条件周围使用括号:

SELECT *
FROM images
WHERE id =  '2650225' AND
    (FIND_IN_SET('sports', Categories ) > 0 OR
     FIND_IN_SET('nightlife', Categories ) >0)
ORDER BY delete_at ASC 
LIMIT 10 
OFFSET 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM