MySQL If statement

I am trying to make this expression If Statement, but I cannot get it to do what I want. If I do select @result, it will give me a value of 0, then why the IF statement is not working?

SET @message = '((sometihng here))'; select LEFT(@message, 1) into @firstChar; select STRCMP(@firstChar,'(') into @result; IF (@result = 0) THEN SET @message = 'true'; //more selects and cals here; END IF; select @message; 

I have to get the truth, but I am not showing me the error:

SQL query: IF( @result =0 ) THEN SET @message = 'true';

MySQL said:

1064 - You have an error in the SQL syntax; check the manual that matches your version of MySQL server for the correct syntax to use near 'IF (@result = 0) THEN SET @message =' true '' on line 1

+4
source share
3 answers

try using the function http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if

 SELECT IF(@result = 0, 'true', '((something here))') AS message 
+7
source

As Max Mara said, good job. The reason IF is not working is not because the syntax is incorrect, but because flow control functions like IF ... THEN only work inside stored procedures or functions, all thanks to @TehShrike

+3
source

IF .. THEN .. ELSE syntax in MySQL is available only for procedural code (stored instructions, functions, triggers ..), but not for SELECT statements.

IF IF USED IN STARTING THE PROCEDURE THE EXAMPLE BELOW

 DELIMITER // CREATE PROCEDURE NAME(IN Number INT) BEGIN IF roll= 1 THEN SELECT * FROM table1 WHERE id = roll; ELSE SELECT * FROM table2 WHERE id = 2; END IF; END // DELIMITER ; 
+3
source

Source: https://habr.com/ru/post/1404019/


All Articles