查询 SQL 集
Querying SQL Set
我有一个 SQL 字段定义为 set('nightlife', 'food', 'sports', 'culture', 'movies', 'general')
现在我想 运行 一个我传递的查询,例如 nightlife,food
,我希望结果包含类别包含夜生活或食物的所有记录。因此,例如带有 nightlife,culture,sports
的记录也应返回,因为它包含夜生活。我不确定如何 运行 这个。我尝试按以下方式使用 IN
关键字:
'SELECT ... FROM table WHERE '$category' IN Categories
但是这不起作用。
更新
运行 按照答案中的建议进行以下查询:
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_set
与 or
一起使用:
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
我有一个 SQL 字段定义为 set('nightlife', 'food', 'sports', 'culture', 'movies', 'general')
现在我想 运行 一个我传递的查询,例如 nightlife,food
,我希望结果包含类别包含夜生活或食物的所有记录。因此,例如带有 nightlife,culture,sports
的记录也应返回,因为它包含夜生活。我不确定如何 运行 这个。我尝试按以下方式使用 IN
关键字:
'SELECT ... FROM table WHERE '$category' IN Categories
但是这不起作用。
更新
运行 按照答案中的建议进行以下查询:
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_set
与 or
一起使用:
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