102

How do I write an IF ELSE statement in a MySQL query?

Something like this:

mysql_query("...(irrelevant code).. IF(action==2&&state==0){state=1}");

Then down in my array I should be able to do this:

 $row['state'] 
//this should equal 1, the query should not change anything in the database, 
//just the variable for returning the information

5 Answers 5

185

You probably want to use a CASE expression.

They look like this:

SELECT col1, col2, (case when (action = 2 and state = 0) 
 THEN
      1 
 ELSE
      0 
 END)
 as state from tbl1;
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, your answer seemed the easier to follow, (as far as where the case is suppose to go in the query, however I have been unable to get it to work properly: "SELECT *, N.id (CASE WHEN (N.action == 2 AND N.state == 0) THEN 1 ELSE 0 END) AS N.state FROM notifications N, posts P WHERE N.userID='$session' AND N.uniqueID=P.id AND P.state='0' AND N.action='1' ORDER BY N.date DESC"
@DylanCross Looks like you might be missing a comma between N.id and (CASE WHEN ...
Ahh, didn't see that, but even when i put the comma it doesn't work.
@DylanCross You might also need to change AS N.state to AS state.
@DylanCross Doesn't MySQL use = instead of == for comparison?
|
34

you must write it in SQL not it C/PHP style

IF( action = 2 AND state = 0, 1, 0 ) AS state

for use in query

IF ( action = 2 AND state = 0 ) THEN SET state = 1

for use in stored procedures or functions

1 Comment

I am unable to get this to work with my code, perhaps my placement is wrong, or something: SELECT *, N.id IF( N.action = 2 AND N.state = 0, 1, 0 ) AS N.state FROM notifications N, posts P WHERE N.userID='$session' AND N.uniqueID=P.id AND P.state='0' AND N.action='1' ORDER BY N.date DESC
21

You're looking for case:

case when action = 2 and state = 0 then 1 else 0 end as state

MySQL has an if syntax (if(action=2 and state=0, 1, 0)), but case is more universal.

Note that the as state there is just aliasing the column. I'm assuming this is in the column list of your SQL query.

1 Comment

This is a much better answer
17
SELECT col1, col2, IF( action = 2 AND state = 0, 1, 0 ) AS state from tbl1;

OR

SELECT col1, col2, (case when (action = 2 and state = 0) then 1 else 0 end) as state from tbl1;

both results will same....

1 Comment

one thing I noticed but couldn't find documentation on it is that the IF has to be the last in the list of columns. If it is first, then it gives an error. Anyone know where to see this in the documentation? Its driving me crazy. WHen I discover something, I like to see it documented for future reference
8

according to the mySQL reference manual this the syntax of using if and else statement :

IF search_condition THEN statement_list
[ELSEIF search_condition THEN statement_list] ...
[ELSE statement_list]
END IF

So regarding your query :

x = IF((action=2)&&(state=0),1,2);

or you can use

IF ((action=2)&&(state=0)) then 
state = 1;
ELSE 
state = 2;
END IF;

There is good example in this link : http://easysolutionweb.com/sql-pl-sql/how-to-use-if-and-else-in-mysql/

2 Comments

A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.
also make sure your example works. otherwise, you're just wasting people's time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.