1

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.

4
  • and how do you identify a specific record of the table1 entity? do you have an id or something? Commented Nov 20, 2017 at 21:24
  • Yes, I have ID (something like Item = 'PA') Commented Nov 20, 2017 at 21:25
  • 1
    Correct! I edited the post.. Commented Nov 20, 2017 at 21:29
  • Iam trying it now.. Commented Nov 20, 2017 at 21:42

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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;

2 Comments

Instead of inserting only 'PA', it put all the rows in ItemAvailability
then your description is ambiguous, see Gordon's answer as he posted it first but I've updated mine to accommodate your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.