MYSQL:如果变量不为空,如何根据变量将变量插入到表中?

我的示例代码:

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的行

转载请注明出处:http://www.sthongjia.com/article/20230526/2068824.html