I have a query that doesn’t work. I’ve fixed the problem by using a single IN statement (the library I’m using to construct my queries was causing this to happen) but I’m still curious why this happens.
The query is as follows:
SELECT * FROM (`table`)
WHERE `field` = 'false'
AND `something` LIKE '%string%'
AND `status_id` IN ('1')
OR `status_id` IN ('2')
OR `status_id` IN ('3')
The query to me makes sense: Select everything from the table where field = false and something is like string and status id is in 1 or status id is in 2 or status id is in 3
When there are multiple IN statements the LIKE portion of the query is completely ignored.
Any ideas why mutliple IN statements would cause the LIKE to be ignored? I can’t work out why: re-arranging the query has no affect (moving the LIKE to the end vs. the start)
Precendence? Shouldn’t you use brackets to differentiate the AND and OR?
status_idIN (‘x’) will if true cause the rest of the expression to be ignored.which is the same as
Try it for yourself by seeing what comes out with this.
Ask yourself what this query should return