Reputation: 3044
Working on a query but I need to implement some if else logic to check for a loop but I keep getting a 1064 bad syntax error.
I've gone over the manual and some examples but still no luck. So I stripped down my query to the most basic comparison below:
SET @start_date = '2019-06-01';
SET @other_date = CURDATE();
IF @start_date < @other_date THEN
Select 'True';
ELSE
Select 'False';
END IF;
The below is the read out I receive from status:
SET @start_date = '2019-06-01' OK Time: 0.065s
SET @other_date = CURDATE()
OK Time: 0.065s
IF @start_date < @other_date THEN Select 'True'
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF @start_date < @other_date THEN Select 'True'' at line 1 Time: 0.067s
I understand that I'm going wrong somewhere with my syntax on the IF clause but in the docs it states:
IF [condition] THEN if-statements ELSE else-statements; END IF;
I'm not sure why this is failing, any insight would be appreciated.
Upvotes: 0
Views: 69
Reputation: 49373
You syntax is false
SET @start_date = '2019-06-01';
SET @other_date = CURDATE();
SELECT
IF (@start_date < @other_date, 'True','False');
The result is true
Upvotes: 1
Reputation: 164069
For queries there is no IF...ELSE...
statement (which you can use in stored procedures/functions).
Instead you can use the function IF()
:
SELECT IF(@start_date < @other_date, 'True', 'False');
or the CASE
expression:
SELECT CASE WHEN @start_date < @other_date THEN 'True' ELSE 'False' END;
See the demo.
Upvotes: 1