0

Update:

I want to insert data conditionally

Why this MariaDB query has errors:

if 2 > 1 then
 select 'hi'
else
 select 'bye'
end if

This is the error:

Error in query (1064): Syntax error near 'else select 'bye' end if' at line 3

if (exists (select * from teachers where name = 'Jennifer'))
    then
        -- do nothing
    else
        insert into teachers (name, age)
        values ('Jennifer', 30)
    end if;

1 Answers1

0

I'm not sure that IF can be used like this outside of a stored procedure or function. You may use the IF() function or a CASE expression instead:

SELECT IF(2 > 1, 'hi', 'bye')

OR

SELECT CASE WHEN 2 > 1 THEN 'hi' ELSE 'bye' END
Tim Biegeleisen
  • 451,927
  • 24
  • 239
  • 318