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
668 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,800 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
• 2,400 points
59 views
0 votes
1 answer
0 votes
1 answer

How do you get the value of a selected option in a dropdown using jQuery?

To get the selected value of an ...READ MORE

answered 4 days ago in Web Development by kavya
24 views
0 votes
1 answer

Do all HTML tags come in a pair?

HTML tags can be of two types: 1.Paired ...READ MORE

answered Jan 16, 2020 in Web Development by anonymous
3,727 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,636 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,830 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,053 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