簡體   English   中英

根據另一個表列更新表列

[英]Update table column on the basis of another table column

我有2張桌子。 'report'和'panel_calculation'是表格。

report contains:

r_zone   r_address    r_status
=======+==========+============
1          8             0
2          9             0
2          6             0
7          9             0
3          2             0


panel_calculation contains:

p_cal_zone  p_cal_address  p_status
===========+==============+============
7                9             1
3                2             1

我需要在'panel_calculation'表的基礎上更新'report'表中的r_status列。

所以最終的結果是這樣的:

Final 'report' should be like this:

r_zone   r_address    r_status
=======+==========+============
1          8             0
2          9             0
2          6             0
7          9             1
3          2             1

我需要你的建議。

即使更新表,您仍然可以加入兩個表。

UPDATE  report s
        INNER JOIN panel_calculation b
            ON s.r_zone = b.p_cal_zone
SET     s.r_status = b.p_status

嘗試這個:

UPDATE report r
INNER JOIN panel_calculation p ON r.r_zone = p.p_cal_zone 
SET r.r_status = p.p_status;

SQL小提琴演示

您可以像這樣使用LEFT JOIN:

UPDATE report t1
LEFT JOIN panel_calculation t2
    ON t1.r_zone=t2.p_cal_zone
    AND t1.r_address=t2.p_cal_address
SET t1.r_status=t2.p_status

作為您的問題的解決方案,請嘗試執行下面的SQL查詢

  update report r set r_status=(select p_status from panel_calculation where p_cal_zone=r.r_zone limit 1 )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM