我试图在搜索即时工作中将3个IN语句与ANd结合起来。 这是一个不完全有效的例子,因为我需要它
SELECT name,thumbnail
FROM table1 WHERE
id IN (SELECT imgid FROM categories WHERE category = 22 )
AND id IN (SELECT imgid FROM namesearch WHERE name LIKE "%car%" )
OR id IN (SELECT imgid FROM tags WHERE tag LIKE "%jet%" )
上面的例子在逻辑上它是在这样一个组中,如果我没有错误
(categorie=22 AND tags=car) OR (namesearch=jet)
如果我搜索在jet
找到的tag
,则不会过滤category=22
如何将语句分组为这样的逻辑
(categorie=22) AND (tags=car OR|AND namesearch=jet)
如果我像这样>> >>
,它会过滤类别SELECT name,thumbnail
FROM table1 WHERE
id IN (SELECT imgid FROM categories WHERE category = 22 )
AND id IN (SELECT imgid FROM namesearch WHERE name LIKE "%car%" )
AND id IN (SELECT imgid FROM tags WHERE tag LIKE "%jet%" )
但是它会在同一时间搜索namesearch或标签吗?
因为如果找到名称搜索,我不想错过标签搜索,我需要它们,但名称搜索是高优先级。
答案 0 :(得分:2)
这是你想要的吗?
SELECT name, thumbnail
FROM table1
WHERE id IN (SELECT imgid FROM categories WHERE category = 22 ) AND
(id IN (SELECT imgid FROM namesearch WHERE name LIKE '%car%' ) OR
id IN (SELECT imgid FROM tags WHERE tag LIKE '%jet%' )
)
请注意,我使用单引号作为字符串常量。这是一种很好的做法。