00 Votes

MySQL: IF-Condition with OR

Question by PC Control | 2015-02-06 at 19:09

I have a MySQL query containing an IF condition which is simplified looking something like this:

SELECT id, IF(col1 = 5, 1, 0) FROM tab

In other words, in case the condition col1 = 5 is met, I would like to get 1, 0 otherwise.

It is working like that. However, it is not that simple, because there should be returned 1 in more than one cases. What I need, is something like an OR conjunction.

So far, I have tried the following syntax:

SELECT id, IF(col1 = (5 OR 8), 1, 0) FROM tab

Unfortunately, this is not working. Does anyone have an idea how to rewrite this query making it running?

ReplyPositiveNegative
1Best Answer1 Vote

Just try the following:

SELECT id, IF(col1 = 5 OR col1 = 8, 1, 0) FROM tab

Doing it like this should work.
2015-02-09 at 15:53

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.