2016-03-05 75 views
1

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

现在我想运行一个查询,我通过例如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 
+0

一种选择是使用'FIND_IN_SET( '夜生活',类别)> 0或FIND_IN_SET( '食物',类别)> 0'。您无法同时搜索多个值 - 您需要使用“或”。 – sgeddes

+0

好吧,如果我在'夜生活,食物'中寻找'夜生活'这项工作还是必须搜索'夜生活,食物'来匹配? – Alk

+0

我认为你需要使用'find_in_set'或'like' - 我不认为'in'会在搜索集合时起作用。 – sgeddes

回答

1

您可以使用find_in_setor

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 
+0

我收到一个错误,请检查更新 – Alk

+0

您不能多次使用'where'关键字... – sgeddes

+0

@mankee - 您还需要在'或'标准周围使用括号。 – sgeddes