简体   繁体   English

MySQL,更新一个表连接多个表

[英]MySQL, UPDATE one Table Joining Multiple Tables

I'm trying to update one table from another that isn´t directly connected, through another table and the statement executes correctly, but it doesn't work because it's updating without data.我正在尝试通过另一个表从另一个未直接连接的表更新一个表,并且该语句正确执行,但它不起作用,因为它在没有数据的情况下进行更新。

The query is:查询是:

UPDATE cities
INNER JOIN footprints ON cities.ID=footprints.ID 
INNER JOIN (SELECT code,id_code,sum(price) AS price FROM buildings GROUP BY code,id_code) AS SUM_Price ON footprints.code=SUM_Price.code AND footprints.id_code=SUM_Price.id_code
SET cities.Total_Price=SUM_Price.price

Add some sample data as example.添加一些示例数据作为示例。

Example of Data tables数据表示例

Does anyone know what is happening?有谁知道发生了什么?

Thanks for your support!谢谢你的支持!

First you must join footprints to buildings and aggregate and then join to cities :首先,您必须将footprints加入buildings并聚合,然后加入cities

UPDATE cities c
INNER JOIN (
  SELECT f.id, sum(b.price) price
  FROM footprints f INNER JOIN buildings b 
  ON f.code = b.code AND f.id_code = b.id_code
  GROUP BY f.id
) s ON s.id = c.id
SET c.total_price = s.price

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

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