tags:

views:

31

answers:

3

What is the property way to insert an IF statement into a mysql query?

SELECT * FROM table WHERE (column1 > 'value')

What if I wanted to put a condition within that query like:

SELECT * FROM table WHERE ((column1 > 'value')and(IF column2 = 'n' THEN WHERE column3 > '5'))
+1  A: 

This should do what you want

SELECT * FROM table 
WHERE (column1 > 'value' and column2 != 'n')
    OR (column1 > 'value' and column2 = 'n' and column3 > '5')
Andrew Cooper
I understand that I could write it this way, but I am try to understand the concept of how if statements are written within a query.
Jason
+1  A: 

how about a case statement?

bluevoodoo1
Is it possible to put a IF expression within a WHERE?
Jason
+2  A: 

Combine your conditions with AND and OR:

 SELECT * FROM table WHERE
     column1 > 'value' AND (column2 <> 'n' OR column3 > '5')
Larry Lustig