100

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
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Dylan Cross
  • 5,918
  • 22
  • 77
  • 118

5 Answers5

184

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;
Community
  • 1
  • 1
Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
  • 2
    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" – Dylan Cross Jan 06 '12 at 20:49
  • 2
    @DylanCross Looks like you might be missing a comma between `N.id` and `(CASE WHEN ...` – Jack Edmonds Jan 06 '12 at 20:51
  • 1
    Ahh, didn't see that, but even when i put the comma it doesn't work. – Dylan Cross Jan 06 '12 at 20:54
  • @DylanCross You might also need to change `AS N.state` to `AS state`. – Jack Edmonds Jan 06 '12 at 20:57
  • You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '== 2 AND N.state == 0) THEN 1 ELSE 0 END) AS state FROM notifications N, posts P' at line 1 – Dylan Cross Jan 06 '12 at 21:09
  • 8
    @DylanCross Doesn't MySQL use `=` instead of `==` for comparison? – Jack Edmonds Jan 06 '12 at 21:18
  • Yes, I had just changed those and it seems to be working, well as far as not returning an error, I think all I have to do now is just play around with the query, as it's sort of a complex thing I'm trying to accomplish. Thanks with your help regarding my question. – Dylan Cross Jan 06 '12 at 21:21
  • how about for other case ? – Yogi Arif Widodo Oct 11 '22 at 06:18
33

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

SergeS
  • 11,533
  • 3
  • 29
  • 35
  • 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 – Dylan Cross Jan 06 '12 at 20:52
19

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.

Eric
  • 92,005
  • 12
  • 114
  • 115
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....

Khandad Niazi
  • 2,326
  • 3
  • 25
  • 22
  • 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 – carinlynchin Jun 23 '16 at 12:42
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/

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Dilraj Singh
  • 951
  • 10
  • 12
  • 2
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) 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.](//stackoverflow.com/help/deleted-answers) – M.A.R. Feb 25 '17 at 12:07
  • 1
    also make sure your example works. otherwise, you're just wasting people's time. – Aries Feb 23 '22 at 04:09