我的示例代码:
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;
我的实际结果:
id title
1 Hello
2 Heyho
3 NULL
我的预期结果:
id title
1 Hello
2 Heyho
如果@VARIABLE2不是空,我只想插入ID为3的行
由于Akina的评论,正确的代码是:
SET @VARIABLE1 := 'Heyho';
create table Test(id integer, title varchar(100));
insert into Test(id, title) values(1, "Hello");
insert into Test(id, title) SELECT 2, @VARIABLE1 WHERE @VARIABLE1 IS NOT NULL;
insert into Test(id, title) SELECT 3, @VARIABLE2 WHERE @VARIABLE2 IS NOT NULL;
select * from Test;
它现在起作用了,我得到了预期的结果!