【发布时间】:2021-06-15 20:52:44
【问题描述】:
我的示例代码:
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 的行
【问题讨论】:
-
insert into Test(id, title) SELECT 3, @VARIABLE2 WHERE @VARIABLE2 IS NOT NULL;dbfiddle.uk/… -
@Akina 非常感谢!现在可以了!
标签: mysql variables null insert