简体   繁体   English

MySql 从另一个表中为每个 id 获取一个字段的 SUM

[英]MySql Get SUM of one field for every id from another table

I have my Sql tables:我有我的 Sql 表:

在此处输入图像描述

I need to do a SUM of the field "Fatturato" for every "IdProdotto" that are connected to "CodiceMercato".我需要为连接到“CodiceMercato”的每个“IdProdotto”字段“Fatturato”做一个总和。

I also tryed this code我也试过这段代码

SELECT prodotti.Fatturato, immettere.CodiceMercato
FROM prodotti INNER JOIN immettere ON immettere.CodiceImmissioni = prodotti.IdProdotto
INNER JOIN (
SELECT SUM(prodotti.Fatturato)
FROM prodotti);

I don't know how to connect the tables, can someone please help me?我不知道如何连接表,有人可以帮我吗?

On your relational drawning, looks like is missing the field idProdotto on immettere table.在您的关系图中,似乎缺少immettere表上的字段idProdotto

But, following the fields in your query example, we can do this:但是,按照查询示例中的字段,我们可以这样做:

SELECT i.CodiceMercato, p.IdProdotto, SUM(p.Fatturato) AS Fatturato
FROM prodotti p
INNER JOIN immettere i ON i.IdProdotto = p.IdProdotto
GROUP BY p.IdProdotto, i.CodiceMercato

I hope you're looking for below -我希望你在下面寻找 -

SELECT prodotti.IdProdotto, SUM(prodotti.Fatturato)
FROM prodotti 
INNER JOIN immettere ON immettere.IdProdotto = prodotti.IdProdotto
INNER JOIN mercati ON immettere.CodiceMercato = mercati.CodiceMercato
group by prodotti.IdProdotto ;

HTH !

暂无
暂无

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

相关问题 MySQL-从另一个表中查询与当前表相同的ID的字段 - mySQL - Querying a field from another table with the same id as in the current one MySQL Join 查询根据另一表中对应的登录ID对一个表中的字段求和 - MySQL Join Query to Sum a Field in One Table based on Its Corresponding Login ID in another Table MYSQL-如何将一个表中的值求和到另一表中的一个字段中? - MYSQL- How to sum values from one table into one field in another table? 将数据从一个mySQL表字段移动到另一个表字段,其中每个表都有一个匹配的ID - Moving data from one mySQL Table field to another Table field where each table has a matching id 如何使用PHP或MySQL对一个表中的不同列求和,并在另一表中获得求和结果? - How to sum different columns from one table and get the sum result in another table using PHP or MySQL? 如何从一个表中获取所有列,而从另一张表中仅获取具有ID的一列? -MySQL - How to get all columns from one table and only one column from another table with ID ? - MySql 如何在一个表中对MYSQL中的一系列行求和,匹配另一个表中的信息以获得结果 - How to Sum a series of rows in MYSQL from one Table, matching the info from another to get the result Mysql从另一个按ID分组的表的总和中更新两列 - Mysql update two columns from a sum of another table grouped by id 如何从phpMyadmin中的一个表中获取ID并将其添加到插入时的另一个表字段中 - How do get a ID from one table in phpMyadmin and add it to another table field on insert MySQL根据另一个表中的列总和触发更新字段 - MySQL triggers to update field based on sum of column from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM