简体   繁体   English

使用SUM的3个表的SQL查询不起作用

[英]SQL Query with 3 Tables using SUM not working

I have 3 tables 我有3张桌子

tbforecasts tbforecasts

creator[varchar] - date[date] - price[double] - status[int] creator [varchar]-日期[date]-价格[double]-状态[int]

tbsector 部门

sector[varchar] - user[varchar] 扇区[varchar]-用户[varchar]

tbgoal 待定

user[varchar] - goal[double] - month[int] - year[int] 用户[varchar]-目标[double]-月[int]-年[int]

I need a query to sum values in month and year selected and group by sector like this: 我需要一个查询来汇总所选月份和年份中的值以及按部门分组的值,如下所示:

sector      sum(price)    sum(goal)<br/>
production  5000.00       30000.00<br/>
sales       42000.00      150000<br/> <br/>

my actual query don't sum the correct value of goal... more than real.. 我的实际查询没有得出目标的正确值……比真实值还多。

SELECT s.sector, sum(m.goal), sum(f.price) AS price SELECT s.sector,sum(m.goal),sum(f.price)AS价格
FROM tbsector AS s 来自tbsector AS
JOIN tbgoals AS m ON m.user = s.user 加入tbgoals AS m ON m.user = s.user
JOIN tbforecasts AS f ON f.creator = s.user 以f.creator = s.user身份加入tbforecasts
WHERE m.month = 6 AND m.year = 2016 月份= 6年份= 2016
AND month(f.date) = 6 AND year(f.date) = 2016 AND month(f.date)= 6 AND year(f.date)= 2016
GROUP BY s.sector; 按部门分组;

If I understand correctly, you need to make LEFT JOINS instead of JOINS 如果我理解正确,则需要创建LEFT JOINS而不是JOINS

SELECT s.sector, sum(m.goal), sum(f.price) AS price 
FROM tbsector AS s 
LEFT JOIN tbgoals AS m ON m.user = s.user 
LEFT JOIN tbforecasts AS f ON f.creator = s.user 
WHERE m.month = 6 AND m.year = 2016 
AND month(f.date) = 6 AND year(f.date) = 2016 
GROUP BY s.sector; 

They need to be summed separately, remember that without the grouping and sums, queries similar to that will match up every tbgoals record with every tbforecasts record, based on same user|creator; 它们需要分别求和,请记住,如果没有分组和求和,类似的查询将基于同一用户将每个tbgoals记录与每个tbforecasts记录进行匹配; so if a "sector" has 2 goals and 3 forecasts, you will get (and sum) 6 results for that sector. 因此,如果一个“部门”有2个目标和3个预测,则您将获得(加和)该部门的6个结果。

There are a number of ways to solve this, the most common are to: put one of the sums in a subquery, and then join that to the other table(s) involve to get the other sum; 解决此问题的方法有很多,最常见的方法是:将一个和放入一个子查询中,然后将其与其他涉及的表连接以获取另一个和; or put both sums into separate subqueries and then join those to the common table. 或将两个总和分别放在单独的子查询中,然后将它们加入公用表。

From the information you've given so far, it is not readily apparent which (if either) would be most accommodating; 从您到目前为止提供的信息来看,哪一个(如果有的话)最适合您尚不容易看出。 as how the data relates (and not just it's structure) can matter. 因为数据如何关联(而不仅仅是结构)很重要。

The second approach I mentioned will look something like this: 我提到的第二种方法如下所示:

SELECT s.sector, totalGoals, totalPrices 
FROM tbsector AS s 
LEFT JOIN (SELECT user, sum(goal) AS totalGoals 
      FROM tbgoals 
      WHERE `month` = 6 AND `year` = 2016
      GROUP BY user
) AS m ON m.user = s.user 
LEFT JOIN (SELECT creator, sum(price) AS totalPrices 
      FROM tbforecasts 
      WHERE month(f.`date`) = 6 AND year(f.`date`) = 2016
) AS f ON f.creator = s.user      
GROUP BY s.sector;

I am guessing this is the more appropriate solution from the original query's WHERE conditions (note that I moved them into the relevant subqueries). 我猜这是从原始查询的WHERE条件开始的更合适的解决方案(请注意,我将它们移到了相关的子查询中)。

Sidenote: If it is not too late in the design process, I would recommend renaming some of your table's fields to not be the same as MySQL keywords/reserved words; 旁注:如果在设计过程中还不算太晚,我建议将表的某些字段重命名为与MySQL关键字/保留字不同; like: date , month , year , technically even user (though that one usually doesn't cause too many issues). 例如: datemonthyear ,从技术上讲甚至是user (尽管通常不会引起太多问题)。

I fix it with the code bellow: 我用下面的代码修复它:


SELECT s.sector, sum(totalGoals), sum(totalPrices) SELECT s.sector,sum(totalGoals),sum(totalPrices)
FROM tbsectores AS s 来自tbsectores AS
LEFT JOIN (SELECT user, sum(goal) AS totalGoals LEFT JOIN(SELECT用户,sum(goal)AS totalGoals
FROM tbgoals 来自tbgoals
WHERE month = 6 AND year = 2016 月份= 6 AND年份= 2016
GROUP BY user 按用户分组
) AS m ON m.user = s.user )AS m ON m.user = s.user
LEFT JOIN (SELECT creator, sum(price) AS >totalPrices LEFT JOIN(选择创建者,sum(price)AS>总价格
FROM tbforecasts 来自tbforecasts
WHERE month(date) = 6 AND year(date) = 2016 月份(日期)= 6和年份(日期)= 2016
GROUP BY creator 创作者分组
) AS f ON f.creator = s.user )AS f ON f.creator = s.user
GROUP BY s.sector; 按部门分组;

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

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