简体   繁体   English

我应该在将mysql查询与三个IN语句或其他内容结合使用时使用AND吗?

[英]Should i use AND while combining mysql query with three IN statements or something else?

Im trying to combine 3 IN statements with ANd in a search im working on. 我试图在搜索即时工作中将3个IN语句与ANd结合起来。 Here is and example that doesnt fully works as i need it to be 这是我不需要完全工作的例子

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%" )

The above example logically it is in a group like this if im not wrong 上面的例子逻辑上它是在这样的组中,如果我没有错

(categorie=22 AND tags=car) OR (namesearch=jet)

And if i search jet that is found at tag it doesnt filter category=22 如果我搜索在tag处找到的jet ,它不会过滤category=22

How can i group the statements in a logic like this 如何在这样的逻辑中对语句进行分组

(categorie=22) AND (tags=car OR|AND namesearch=jet)

It filters category if i put an AND like this 如果我像这样放置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%" )
   AND id IN (SELECT imgid FROM tags WHERE tag LIKE "%jet%" )


But will it search in namesearch or tags or at both in the same time? 但它会在同一时间搜索namesearch或标签或两者吗?

Because i dont want to miss tag search if a namesearch is found, i need them both, but namesearch is in high priority. 因为如果找到名称搜索,我不想错过标签搜索,我需要它们,但名称搜索是高优先级。

Is this what you want? 这是你想要的吗?

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%' )
      )

Notice that I used single quotes for the string constants. 请注意,我使用单引号作为字符串常量。 This is good practice. 这是一种很好的做法。

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

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