根据另一个表列更新表列

时间:2012-11-25 07:15:16

标签: php mysql compare

我有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

我需要你的建议。

4 个答案:

答案 0 :(得分:1)

即使更新表格,您仍然可以加入这两个表格。

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

答案 1 :(得分:1)

试试这个:

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

SQL Fiddle Demo

答案 2 :(得分:0)

您可以像这样使用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

答案 3 :(得分:0)

作为您问题的解决方案,请尝试执行以下sql查询

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