ITTONE Création des applications Web

MYSQL: How can insert into a table a variable, depending on the variable if its not null?

My example code:

SET @VARIABLE1 := 'Heyho';
create table Test(id integer, title varchar(100));
insert into Test(id, title) values(1, "Hello");
insert into Test(id, title) values(2, (SELECT @VARIABLE1));
insert into Test(id, title) values(3, (SELECT @VARIABLE2 WHERE @VARIABLE2 IS NOT NULL));
select * from Test;

My actual results:

id  title
1   Hello
2   Heyho
3   NULL

My expected results:

id  title
1   Hello
2   Heyho

I only want to insert my row with ID 3 if the @VARIABLE2 Is not null

Product of the Month September 2016

Source link

Leave a Comment

Your email address will not be published. Required fields are marked *