CASE
is a possibility. But shorter to write would be IF()
(at least with MySQL):
SELECT *, IF(COND2, 1, 0) AS cond2, IF(COND3, 1, 0) as cond3 ...
and every cond*
matched if its value is 1
.
Of course it does not make sense to check whether COND1
matches or not. For all results you get back, COND1
was true. So IF(COND1, 1, 0)
will always return 1
(in your example).
Update:
Again at least for MySQL, I discovered, that the following is sufficient if you only want to get either 1
or 0
as result:
SELECT *, COND2 AS cond2, COND3 as cond3 ...
and with respect to Mark's answer, you can avoid writing the conditions twice, if you use HAVING
instead of WHERE
(HAVING
as access to aliases):
SELECT *, COND2 AS cond2, COND3 as cond3
FROM table
HAVING COND1 AND (cond2 OR cond3)
(note: uppercase means the actual condition expression and lowercase is the alias name)
Update 2:
Well it changes not that much: you only have to check conditions that are connected via OR
in your updated version it would be sufficient to check whether COND2
is true or not. If so, then COND3
is also true and vice versa.
SELECT *, COND1 AS cond1, COND2 as cond2, COND4 as cond4
FROM table
HAVING cond1 OR (cond2 AND COND3) OR cond4
I think the point is clear.