Teddy
Teddy

Reputation: 37

Update record in a table where the condition depends on other table

I have 2 table in MySQL

I need a SQL Syntax which can update or edit ProjectStatus in table Project

if the project is in the quotation table then change ProjectStatus to 1 if the project is NOT in the quotation table then change ProjectStatus to 2

So after i runt that SQL syntax, the Project table will become like this:

I tried many queries, but i cant figure it out.

Anyone can help please.

Thank you for your help.

Upvotes: 0

Views: 183

Answers (2)

Barmar
Barmar

Reputation: 781833

UPDATE Project p
LEFT JOIN Quotation q
ON p.ProjectID = q.ProjectID
SET ProjectStatus = IF(q.ProjectID IS NULL, 2, 1)

Upvotes: 1

Kickstart
Kickstart

Reputation: 21533

UPDATE Project
LEFT OUTER JOIN Quotation
ON Project.ProjectID = Quotation.ProjectID
SET Project.ProjectStatus = IF(Quotation.ProjectID IS NULL, 2, 1)

Upvotes: 1

Related Questions