How do write IF ELSE statement in a MySQL query

0 votes

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

Feb 18, 2022 in Web Development by Rahul
• 9,680 points
707 views

1 answer to this question.

0 votes

You probably want to use a CASE expression which looks like this:

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

answered Feb 18, 2022 by Aditya
• 7,680 points

Related Questions In Web Development

0 votes
0 answers

If/else else if in Jquery for a condition

I am having a set of text ...READ MORE

Jul 1, 2022 in Web Development by gaurav
• 23,260 points
2,821 views
0 votes
0 answers

How do you implement role-based access control (RBAC) in a full stack application?

How do you implement role-based access control ...READ MORE

Oct 14 in Web Development by anonymous
• 3,820 points
71 views
0 votes
1 answer
0 votes
1 answer
0 votes
0 answers

How do I fix performance issues caused by event delegation in a large DOM tree using jQuery?

How do I fix performance issues caused ...READ MORE

Nov 19 in Web Development by Nidhi
• 3,820 points
34 views
0 votes
0 answers

Using if(isset($_POST['submit'])) to not display echo when script is open is not working

I'm running into a small issue with ...READ MORE

Aug 1, 2022 in PHP by Kithuzzz
• 38,000 points
2,663 views
0 votes
1 answer

Want a command to be executed in Salt only if a directory is empty

You should consider using this inside your ...READ MORE

answered Jun 12, 2018 in DevOps Tools by Damon Salvatore
• 5,980 points
1,850 views
0 votes
1 answer

Creating A New MySQL User In Amazon RDS Environment

AWS RDS security groups documentation (a common ...READ MORE

answered Jul 18, 2018 in AWS by Priyaj
• 58,020 points
2,078 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP