I have a column containing rows of pipe separated STRING values:
| colA |
___________
| 5|4|2|255 |
| 5|4|4|0 |
| 5|4|4|3 |
| 5|4|4|4 |
I need to create a query that will select all rows that contain 4 or 5, but never 2 or 3. Something along the lines of:
SELECT t.colA
FROM my_table t
WHERE (t IN ("4", "5") AND t NOT IN ("2","3")
Resulting in:
| colA |
___________
| 5|4|4|0 |
| 5|4|4|4 |
I ended up using a combination of the two answers below, as using either method alone still left me with rows containing only "255". Here's the final query:
SELECT t.colA
FROM my_table t
WHERE (t.colA IN ('4', '5') OR t.colA LIKE "%|5|%"
OR t.colA LIKE "%|5" OR t.colA LIKE "5|%")
AND t.colA NOT LIKE "%3%"
AND t.colA NOT LIKE "%|2|%"
AND t.colA NOT REGEXP "^2|%"
AND t.colA NOT REGEXP "%|2$"
There is probably a more elegant way to do this, but that does the trick.
What about using the LIKE
function ?
where (t like '%4%' or t like '%5%')
and (t not like '%2%' and t not like '%3%')
That should do the job.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments