Reputation: 4945
I have a sql statement that I already know is not structured correctly using a SELECT
with AND
and OR
. I have tried several variations but I can't seem to get it to work. It is returning 18 records when it should be returning only 17.
SELECT *
FROM xxx_activity
WHERE (dest_id = '20321' OR dest_id = 0)
AND user_id != '20321'
AND department_id = 14
OR department_id = 15
OR department_id = 16
AND read_test NOT LIKE '%|20321|%'
AND org_code = '20321'
Let me break down what I need
dest_id
can only equal '20321' OR '0'
user_id
must NOT equal 20321
department_id
can only equal 14 OR 15 OR 16
read_test
must NOT be like %|20321|%
org_code
must equal 20321
Any help to structure this correctly?
Upvotes: 0
Views: 33
Reputation: 77866
You are probably not grouping your condition properly
WHERE dest_id in ('20321' ,'0')
AND user_id != '20321'
AND department_id in (14 , 15 , 16)
AND read_test NOT LIKE '%|20321|%'
AND org_code = '20321'
Upvotes: 1
Reputation: 1171
You didn't mention conditions for code_org, but i think this is what you want:
SELECT
*
FROM
xxx_activity
WHERE
dest_id in ('20321' , '0')
AND
user_id != '20321'
AND
department_id in (14 , 15 , 16)
AND
read_test NOT LIKE '%|20321|%'
AND
org_code = '20321'
Upvotes: 1