How to write IF ELSE statement in a MySQL query?
This is a MySQL IF ELSE statement that uses boolean expressions, but there are often used in programming languages and by programmers. If you aren't familiar with them, booleans are essentially identifiers that are equivalent to True or False. You can have the statement IF temperature < 50 (which is equal to True), but if it's equal to or greater than 50 (which is false). This is an IF ELSE statement, which means that it requires two comparisons: one for the IF part and one for the ELSE part.
Also Check these Questions:
insert if not exists sql server
How to drop a database if it exists in MySQL?
How to modify an existing column in SQL?
mysql select else if
select id,
(
CASE
WHEN qty_1 <= '23' THEN price
WHEN '23' > qty_1 && qty_2 <= '23' THEN price_2
WHEN '23' > qty_2 && qty_3 <= '23' THEN price_3
WHEN '23' > qty_3 THEN price_4
ELSE 1
END) AS total
from product;
Source: stackoverflow.com
mysql else if
IF condition1 THEN
statements;
ELSEIF condition2 THEN # OPTIONAL
statements;
ELSE # OPTIONAL
statements;
END IF;
mysql if else
IF condition THEN
statements;
ELSE
else-statements;
END IF;
Source: www.mysqltutorial.org
In the above SQL statement, I have used ELSE IF. ELSE IF means that if the first IF condition is false, then check the second IF condition and execute this.