组合IN和AND查询

Nav*_*mar 1 mysql sql

我的db表如下

TestTable的

|-----|-------------------|
  Id     comment
   2  | 20 degree celsius
   3  | 30 degree Farenheit
   4  | 40 degree Farenheit
   5  | sometime 
   6  | plain text
   7  | plain text
   8  | plain text
Run Code Online (Sandbox Code Playgroud)

我想查询db以获取string包含celsius或farenheit的所有id

查询:

SELECT Id FROM testTable where id in (2,3,4) 
AND comment like '%celsius%' or comment like '%Farenheit%'
Run Code Online (Sandbox Code Playgroud)

但是这个查询检查所有的ID,而不是我在条件中提供的id?如何只查询条件中的id?

him*_*056 8

只需使用这样的括号:

SELECT Id FROM testTable where id in (2,3,4) 
AND 
(comment like '%celsius%' or comment like '%Farenheit%')
Run Code Online (Sandbox Code Playgroud)

看到这个SQLFiddle

  • +1.对不起,直到更新才看到你的答案! (2认同)