我有2张桌子。"报告"和"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"表的基础上更新"报告"表中的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
- SQLFiddle 演示
试试这个:
UPDATE report r
INNER JOIN panel_calculation p ON r.r_zone = p.p_cal_zone
SET r.r_status = p.p_status;
SQL 小提琴演示
作为问题的解决方案,请尝试执行以下sql查询
update report r set r_status=(select p_status from panel_calculation where p_cal_zone=r.r_zone limit 1 )
你可以像这样使用 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