I have the values of 'foo', 'bar' and 'buz'. I'd like to select rows 1 and 3 from the following table:
+----+------+------+------+
| Id | Col1 | Col2 | Col3 |
+----+------+------+------+
| 1 | foo | | |
| 2 | foo | bar | buz |
| 3 | | bar | buz |
+----+------+------+------+
What would be the optimal MySQL query for that considering the following:
- empty spaces are null
- I have n columns where 4 < n < 20
- there will be couple hundred rows
- I will be selecting particular combinations of columns in a row where only provided values can be set and the rest needs to be null. E.g.: I need a query of this kind
SELECT * FROM my_table WHERE Col1 = foo OR (Col2 = bar AND Col3 = buz);
that'll return only rows 1 and 3.
In other words I'd like to have a query that returns rows only with certain combinations of values, not the rows where coln = 'some_val'
and we dont' care about the others.
The reason for this is that I'd like to have 5 combinations in 1 query and I'd like to avoid writing (...) coln <> NULL(...)
everywhere.
I'd appreciate your suggestions.