开发者

mySql: `SELECT`` statement with `OR` and `AND`

开发者 https://www.devze.com 2023-03-18 02:53 出处:网络
I am confused about why I am getting rows returned from a query... Here is the query, it searches for the existence of a string in a single table.

I am confused about why I am getting rows returned from a query...

Here is the query, it searches for the existence of a string in a single table.

SELECT * 
  FROM `donor` 
 WHERE `col1` LIKE '%test%' 
    OR `开发者_JS百科col2` LIKE '%test%' 
    OR `col3` LIKE '%test%' 
    OR `col4` LIKE '%test%' 
   AND `delete` = 0

The last line AND delete = 0 is returning rows whose 'delete' column is '1'? Does anyone have an idea of why this is happening?


Rewrite the WHERE clause as follows:

SELECT * FROM `donor` WHERE 
(`col1` LIKE '%test%' OR 
`col2` LIKE '%test%' OR 
`col3` LIKE '%test%' OR 
`col4` LIKE '%test%')
AND `delete` =0

You want to or the first set of criteria together and then perform the AND operation.


You'll need to group the conditions together by enclosing them in brackets (). Try this:

SELECT * FROM `donor` WHERE 
(`col1` LIKE '%test%' OR 
`col2` LIKE '%test%' OR 
`col3` LIKE '%test%' OR 
`col4` LIKE '%test%') AND
`delete` =0
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号