简体   繁体   中英

Update based of 1st and 2nd table

I have 3 tables.

1st table – MainTable - tableA

Have Project Name and description

A   Apple
B   Banana
C   Carrot

2nd table - Table B Child table :

A.10
A.20
A.30
B.10
B.20
B.30

Name of project (A, B, C) is Foreign key to table Child.

I have to update third table (table C) based on the

A …Apple
B…Banana
C…Carrot 

This is Working fine with inner join.

Now when I am doing updates on code 10, 20 and 30 .. with ref to A, B and C of 10,20 and 30 It is not working.

Here is the query I wrote which is working fine to Update A, B and C

UPDATE C
   SET c.[ProjectName] = a.[sysprojectname] 
   FROM TableC C
   inner join tableA a ON c.[CostOBJProject]=a.[workpackageid]
   Where c.[ProjectName] is null or c.[ProjectName]=''

So question is – I have to update table C based on value of table B with foreign key to table A.

in case I have value A and 10 in the tableC , then it should update the description in tableC .

Please check this link, this will help you

https://dev.mysql.com/doc/refman/5.7/en/update.html

I think you have problem on your syntax. You can perform connected tables inside the UPDATE query.

UPDATE TableC, tableA
   SET TableC.[ProjectName] = tableA.[sysprojectname] 
   Where   tableA.connectcolumn =  TableC.connectcolumn
   AND (TableC.[ProjectName] is null or TableC.[ProjectName]='' )

With update syntax it has to be strict, therefore LEFT JOIN is not allowed on this scenario. It is equivalent to EQUI JOIN, an old style of joining.

SET clause commands the interpreter to manipulate only that column, even tho both columns were called. With the complexity of your code you added OR logic command, would be nice to add parenthesis to properly utilize the logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM