MYSQL-插入一个空值,如何避免?

MYSQL-插入一个空值,如何避免?,mysql,insert,exists,Mysql,Insert,Exists,我有一个SQL查询,它看起来像: insert into tableA(order_id, contractnotenumber, shipmentdate, comment) values ((select entity_id from tableb where increment_id = '12345678'), '', '1970-01-01', ''); 现在,如果子查询从表B中选择entity_id,其中increment_id='12345678'返回null

我有一个SQL查询,它看起来像:

insert into tableA(order_id, contractnotenumber, shipmentdate, comment) 
    values ((select entity_id from tableb where increment_id = '12345678'), 
    '', '1970-01-01', '');
现在,如果子查询从表B中选择entity_id,其中increment_id='12345678'返回null,则插入不起作用。我想要的是一种简单的方式,即如果子查询返回null,则不要执行任何操作。我尝试了insert ignore,但这将插入0而不是null的行,这是可行的,但不是我想要的。在SQL Server中,我使用了if not existsselect…,但这不适用于MYSQL

想法

谢谢

insert into tableA(order_id, contractnotenumber, shipmentdate, comment)
select entity_id, '', '1970-01-01', '' from tableb where increment_id = '12345678'
如果在表B中找不到该行,则不会插入任何内容


如果在表B中找不到该行,则不会插入任何内容。您也可以尝试mysql>insert ignore

您也可以尝试mysql>insert ignore

Hmmm,不知何故,查询将永远运行……缺少某些细节?select本身返回数据。@EOB:在此查询中,INSERT插入select选择的内容。如果它没有返回任何内容-然后立即插入退出是的,退出有效,但如果存在某个内容,它会一直运行,一直运行,直到我取消它或得到超时。啊,我的错,我以前尝试过其他事务,它阻止了新的查询!解决了的!嗯,不知怎的,查询永远运行着……缺少一些细节?select本身返回数据。@EOB:在此查询中,INSERT插入select选择的内容。如果它没有返回任何内容-然后立即插入退出是的,退出有效,但如果存在某个内容,它会一直运行,一直运行,直到我取消它或得到超时。啊,我的错,我以前尝试过其他事务,它阻止了新的查询!解决了的!正如我所说,这在我的情况下不起作用,它将插入id为0的行,这不是我想要的。正如我所说,这在我的情况下不起作用,它将插入id为0的行,这不是我想要的。