Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mariadb IF statements error message?

I test sql querys.

    IF 3 = 3 THEN 
       SELECT 'TRUE'
    ELSE
       SELECT 'FALSE'
    END IF;

I excuted that query on SQLyog.

I got the error message below.

 You have an error in your SQL syntax; check the manual that corresponds to 
 your MariaDB server version for the right syntax to use near 'ELSE SELECT 
 'FALSE' END IF' at line 3

That query is very simple. I don't know why happen error message.

like image 591
Mr.Park Avatar asked Jan 27 '18 03:01

Mr.Park


People also ask

How do I show warnings in MariaDB?

The \W command will show warnings after every statement, while \w will disable this. Starting the client with the --show-warnings option will show warnings after every statement. MariaDB 10.3.

When ELSE MariaDB?

In MariaDB, the IF-THEN-ELSE statement is used to execute code when a condition is TRUE, or execute different code if the condition evaluates to FALSE.

Is null in MariaDB?

Introduction to MariaDB is null operatorThe is null returns true if the expression is null ; otherwise, it returns false . Note that in MariaDB, a value of 0 is considered false and non-zero values considered true . The expression returns true if the value is not null; otherwise, it returns false .


1 Answers

IF itself is not a query, it cannot be executed as a standalone statement.

There are two different IFs that you can use.

One is the conditional construct in compound statements. It can be used in stored routines:

DELIMITER $
CREATE PROCEDURE pr()
BEGIN
    IF 3 = 3 THEN 
       SELECT 'TRUE';
    ELSE
       SELECT 'FALSE';
    END IF;
END $
DELIMITER ;
CALL pr;

Or, if you are running a reasonably new version of MariaDB (10.1+), you can also use it in an anonymous block:

DELIMITER $
BEGIN NOT ATOMIC
    IF 3 = 3 THEN 
       SELECT 'TRUE';
    ELSE
       SELECT 'FALSE';
    END IF;
END $
DELIMITER ;

Another is the IF function, which can be used inside a query:

SELECT IF(3 = 3 THEN 'TRUE','FALSE');
like image 126
elenst Avatar answered Jan 02 '23 16:01

elenst