MYSQL - 插入和null值,如何避免?

时间:2012-05-07 09:03:31

标签: 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', '');

现在,如果子查询((从tableb中选择entity_id,其中increment_id ='12345678'))返回null,则插入不起作用。我想要的是一个简单的方法,如果子查询返回null,不要做任何事情。我尝试插入忽略,但这会插入行而不是null,这有效,但它不是我想要的。在SQL Server中我使用如果不存在(选择...),但这不适用于MYSQL。

想法?

谢谢!

2 个答案:

答案 0 :(得分:1)

insert into tableA(order_id, contractnotenumber, shipmentdate, comment)
select entity_id, '', '1970-01-01', '' from tableb where increment_id = '12345678'

如果在tableb

中找不到行,则不会插入任何内容

答案 1 :(得分:0)

你也可以试试mysql>插入忽略