简体   繁体   中英

How to update a column pertaining to MySQL table with values from another column in another table using inner join?

Event table (initial) :

在此处输入图片说明

status table:

在此处输入图片说明

table ready_reason:

在此处输入图片说明

table delay_reason :

在此处输入图片说明

table spare_reason:

在此处输入图片说明

table down_reason :

在此处输入图片说明

Required output

在此处输入图片说明

So basically, I've to replace the values in the columns status code and reason code in the event table with the corresponding values from the tables below.

I tried all sorts of permutation and combinations of inner joins. However, couldn't crack it. Would appreciate any insights.

My code to update status code column (didn't work) :

update event eve
set eve.status_code = sta.name
inner join status sta on eve.status_code = sta.status_code

Since reason_ code column in event table needs to be update using multiple table, I'm unable to come up with code for this one

Instead of updating the names to the code fields you could create a view.

The example below uses LEFT JOIN's on the reason tables, and then COALESCE's the reason names.

CREATE VIEW vwEvent AS
SELECT 
 ev.*, 
 COALESCE(st.Name, ev.`Status Code`) AS `Status Name`, 
 COALESCE(ready.Name, delay.Name, spare.Name, down.Name, ev.`Reason Code`) AS `Reason Name`
FROM `event` ev
LEFT JOIN `status` AS st ON st.`Status Code` = ev.`Status Code`
LEFT JOIN ready_reason AS ready ON (ev.`Status Code` = 'R'  AND ready.`Status Code` = ev.`Reason Code`)
LEFT JOIN delay_reason AS delay ON (ev.`Status Code` = 'D1' AND delay.`Status Code` = ev.`Reason Code`)
LEFT JOIN spare_reason AS spare ON (ev.`Status Code` = 'S'  AND spare.`Status Code` = ev.`Reason Code`)
LEFT JOIN down_reason  AS down  ON (ev.`Status Code` = 'D2' AND down.`Status Code`  = ev.`Reason Code`);

Then select from the view to get the status and reason names:

select EventId, Duration, `Status Name`, `Reason Name`
from vwEvent

Test on db<>fiddle here

Well, I suppose you could use the view to update the table itself.
But that might not be the best idea.

update `event` ev
join `vwEvent` vw on vw.EventId = ev.EventId
set ev.`Status Code` = vw.`Status Name`,
    ev.`Reason Code` = vw.`Reason Name`

Do an update using LEFT JOIN on "reason" tables and COALESCE on the name column from those tables

UPDATE event e 
JOIN status s ON s.status_code = e.status_code
LEFT JOIN ready_reason r ON r.status_code = e.reason_code AND e.status_code = 'R'
LEFT JOIN delay_reason d ON d.status_code = e.reason_code AND e.status_code = 'D1'
LEFT JOIN spare_reason sp ON sp.status_code = e.reason_code 
LEFT JOIN down_reason do ON do.status_code = e.reason_code
SET e.status_code = s.name, e.reason_code = COALESCE(r.name, d.name, sp.name, do.name)

使用连接,例如,我从上面使用了2个表连接,就可以像下面这样用普通列完成所有表连接

SELECT * FROM Event table e LEFT JOIN status s ON e.status_code=s.status_code  

Assuming the values in your tables are static, following code can update base on CASE-WHEN in MySQL.

update event eve
set eve.status_code = (SELECT name from status_table WHERE status_code = eve.status_code), eve.reason_code =   CASE

WHEN eve.status_code = 'R' THEN (SELECT name from ready_reason WHERE status_code = eve.reason_code)

WHEN eve.status_code = 'D1' THEN (SELECT name from delay_reason WHERE status_code = eve.reason_code)

WHEN eve.status_code = 'S' THEN (SELECT name from spare_reason WHERE status_code = eve.reason_code)

WHEN eve.status_code = 'D2' THEN (SELECT name from down_reason WHERE status_code = eve.reason_code)

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