简体   繁体   English

如何使用内部联接用另一个表中另一列的值更新与MySQL表有关的列?

[英]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: 表ready_reason:

在此处输入图片说明

table delay_reason : 表delay_reason:

在此处输入图片说明

table spare_reason: 表spare_reason:

在此处输入图片说明

table down_reason : 表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 由于事件表中的reason_ code列需要使用多个表进行更新,因此我无法为此代码提供代码

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. 下面的示例在原因表上使用LEFT JOIN,然后在原因名称上使用COALESCE。

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 DB <>小提琴测试这里

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 使用“原因”表上的LEFT JOIN和这些表的name列上的COALESCE进行更新

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. 假设表中的值是静态的,则以下代码可以基于MySQL中的CASE-WHEN更新。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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