Grendizer
Grendizer

Reputation: 292

MySQL: How to INSERT VALUE INTO TABLE if a value in another table is NULL?

I have:

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

I want to execute the above query ONLY IF ItemBeingTracked in ItemAvailability is NULL.

Edit: If ItemAvailability.ItemBeingTracked == NULL Then do the above query.

Upvotes: 1

Views: 41

Answers (2)

Ousmane D.
Ousmane D.

Reputation: 56499

a bit rusty with SQL, but I believe this is one way to go about it.

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

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1271231

I think you would just add the condition to the WHERE:

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

Upvotes: 2

Related Questions