Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)

In a MySQL query, how do I write an IF ELSE statement?

Something like below:

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

Then down in my array, I can do this:

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

1 Answer

0 votes
by (12.7k points)
edited by

You may need to use a CASE expression.

It can be like this:

SELECT col1, col2, (case when (action = 2 and state = 0) 
 THEN
      1 
 ELSE
      0 
 END)
 as state from tbl1;

Do refer to the SQL tutorial that will help you out in a better way.

Related questions

0 votes
1 answer
asked Dec 26, 2020 in SQL by Appu (6.1k points)
+2 votes
1 answer
0 votes
2 answers

Browse Categories

...