I've started learning SQL and I'm trying to reduce my repetition. Can someone help with this?
SELECT email
FROM users
WHERE campaign LIKE 'BBB-2'
AND test ='lions'
OR test = 'tigers'
OR test = 'bears';
I've started learning SQL and I'm trying to reduce my repetition. Can someone help with this?
SELECT email
FROM users
WHERE campaign LIKE 'BBB-2'
AND test ='lions'
OR test = 'tigers'
OR test = 'bears';
Use in
:
SELECT email
FROM users
WHERE campaign = 'BBB-2' AND test IN ('lions' , 'tigers' , 'bears');
Notes:
this solves a logical prescendence issue in your original query: the OR
ed condition needed to be surrounded by parentheses
I replaced the LIKE
condition with an equality check; there is no point using LIKE
when the right operand contains no wildcard
Use IN
:
WHERE campaign LIKE 'BBB-2' AND
test IN ('lions' , 'tigers' , 'bears');
As a bonus this will fix a logic bug in your query. Presumably, you want the campaign
condition to filter all rows, not just those with 'lions'
.
You could use the below:
SELECT email
FROM users
WHERE campaign LIKE 'BBB-2'
AND test IN('lions','tigers','bears')