MySQL:如果另一个表中的值为NULL,如何INS VALUE IN IN TABLE?

时间:2017-11-20 21:19:26

标签: mysql sql

我有:

INSERT INTO ItemTracking(`Item`, `Source`, `LeftSourceAt`)
SELECT `Item`, `Source`, now() FROM ItemAvailability WHERE Item = 'PA';

我想在ItemBeingTracked ItemAvailability中执行上述查询 NULL

修改:If ItemAvailability.ItemBeingTracked == NULL Then执行上述查询。

2 个答案:

答案 0 :(得分:2)

我认为您只需将条件添加到WHERE

INSERT INTO ItemTracking(Item, `Source`, LeftSourceAt)
    SELECT Item, `Source`, now()
    FROM ItemAvailability
    WHERE Item = 'PA' AND ItemBeingTracked IS NULL;

答案 1 :(得分:1)

SQL有点生疏,但我相信这是一种解决方法。

INSERT INTO ItemTracking(`Item`, `Source`, `LeftSourceAt`)
SELECT `Item`, `Source`, now() 
FROM ItemAvailability 
WHERE Item = 'PA' AND ItemBeingTracked IS NULL;