Reputation: 23
SELECT * FROM foobar
WHERE userid != '100' AND col1 REGEXP '[[:<:]]test[[:>:]]'
OR userid != '100' AND col2 REGEXP '[[:<:]]test[[:>:]]'
OR userid != '100' AND col3 REGEXP '[[:<:]]test[[:>:]]'
This query is working fine for me. It will filter basically on two criteria
I have another col4
which I want that, other than the above two condition
, it must filter out those results where col4 = 'y'
How should I modify this above query?
Upvotes: 2
Views: 68
Reputation: 69953
Something like this? You could also separate out the userid != '100'
since it is common to the three checks.
SELECT *
FROM foobar
WHERE userid != '100'
AND (col1 REGEXP '[[:<:]]test[[:>:]]'
OR col2 REGEXP '[[:<:]]test[[:>:]]'
OR col3 REGEXP '[[:<:]]test[[:>:]]' )
AND col4 != 'y'
Upvotes: 1
Reputation: 9784
SELECT * FROM foobar WHERE
(userid != '100') AND
(col1 REGEXP '[[:<:]]test[[:>:]]' OR userid != '100' OR col2 REGEXP '[[:<:]]test[[:>:]]' OR col2 REGEXP '[[:<:]]test[[:>:]]')
AND col4 <> 'y'
Upvotes: 0
Reputation: 82893
Try this:
SELECT *
FROM foobar
WHERE col4 = 'y'
OR (
userid != '100'
AND (
col1 REGEXP '[[:<:]]test[[:>:]]'
OR
col2 REGEXP '[[:<:]]test[[:>:]]'
OR
col2 REGEXP '[[:<:]]test[[:>:]]'
)
)
Upvotes: 0
Reputation: 135729
SELECT *
FROM foobar
WHERE userid != '100'
AND (col1 REGEXP '[[:<:]]test[[:>:]]'
OR col2 REGEXP '[[:<:]]test[[:>:]]'
OR col3 REGEXP '[[:<:]]test[[:>:]]')
AND col4 <> 'y'
Upvotes: 0
Reputation: 837946
You have an error in your query, You need to change the second col2
to col3
. It's easier to see the error if you reformat your code:
SELECT * FROM foobar
WHERE (userid != '100' AND col1 REGEXP '[[:<:]]test[[:>:]]') OR
(userid != '100' AND col2 REGEXP '[[:<:]]test[[:>:]]') OR
(userid != '100' AND col2 REGEXP '[[:<:]]test[[:>:]]')
I've also added parentheses to make the evaluation order clear. You can rewrite your query to avoid repeating the expression userid != '100'
. Then just add your missing clause:
SELECT * FROM foobar
WHERE userid != '100'
AND col4 <> 'y'
AND (
col1 REGEXP '[[:<:]]test[[:>:]]' OR
col2 REGEXP '[[:<:]]test[[:>:]]' OR
col3 REGEXP '[[:<:]]test[[:>:]]'
)
Upvotes: 2