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?
Related Topics
MySQL: Change minimum word length for full text search
Tip | 1 Comment
MySQL: CSV Export as File stored on the Server
Tutorial | 0 Comments
MySQL: CSV Export as automatic Download
Tutorial | 0 Comments
MySQL: Regular Expressions in MySQL queries
Info | 0 Comments
MySQL: Combine full text search with LIKE search for words with 3 letters
Tutorial | 2 Comments
MySQL/PHP: How to retrieve the last INSERT ID
Info | 0 Comments
PHP: Colorize Table Rows Alternately with PHP
Tutorial | 0 Comments
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.
Just try the following:
Doing it like this should work.
2015-02-09 at 15:53