我有两个需要连接的表...我想在“id”上连接 table1 和 table2 - 但是在表 2 中 id 不是唯一的。我只希望为表二返回一个值,该值表示名为“total_sold”的列的总和 - 在指定的日期范围内(比如一个月),但是我希望同时有多个日期范围...
SELECT ta.id, sum(tb.total_sold) as total_sold_this_week, sum(tc.total_sold) as total_sold_this_month
FROM table_a as ta
LEFT JOIN table_b as tb ON ta.id=tb.id AND tb.date_sold BETWEEN ADDDATE(NOW(),INTERVAL -1 WEEK) AND NOW()
LEFT JOIN table_b as tc ON ta.id=tc.id AND tc.date_sold BETWEEN ADDDATE(NOW(),INTERVAL -1 MONTH) AND NOW()
GROUP BY ta.id
这有效,但不会对行求和 - 只为每个 id 返回一行...我如何从表 b 中获取总和而不是仅一行???
如果问题的格式需要更多的工作,请批评 - 如果需要,我可以重写并提供示例数据 - 这是一个更大问题的简单版本。
-谢谢
I have two tables that I need to join... I want to join table1 and table2 on 'id' - however in table two id is not unique. I only want one value returned for table two, and this value represents the sum of a column called 'total_sold' - within a specified date range (say one month), however I want more than one date range at the same time...
SELECT ta.id, sum(tb.total_sold) as total_sold_this_week, sum(tc.total_sold) as total_sold_this_month
FROM table_a as ta
LEFT JOIN table_b as tb ON ta.id=tb.id AND tb.date_sold BETWEEN ADDDATE(NOW(),INTERVAL -1 WEEK) AND NOW()
LEFT JOIN table_b as tc ON ta.id=tc.id AND tc.date_sold BETWEEN ADDDATE(NOW(),INTERVAL -1 MONTH) AND NOW()
GROUP BY ta.id
this works but does not SUM the rows - only returning one row for each id... how do I get the sum from table b instead of only one row???
Please criticise if format of question could use more work - I can rewrite and provide sample data if required - this is a trivialised version of a much larger problem.
-Thanks
发布评论
评论(1)
使用子查询
解决这个问题的一种方法是使用子查询。
LEFT JOIN
为右表中的每个匹配创建一个新的“结果”,因此使用两个 LEFT JOIN 会创建比您想要的更多的行。您可以仅子选择所需的值,但这可能会很慢:结果:
使用 SUM(CASE ...)
此方法不使用子查询(并且在较大的数据集上可能会更快)。我们希望使用“最大”日期范围将 table_a 和 table_b 连接在一起一次,然后使用基于 SUM() /doc/refman/5.0/en/case-statement.html" rel="noreferrer">
CASE
计算“较小范围”。这将返回与子查询示例几乎相同的结果集:
唯一的区别是
0
而不是NULL
。您可以使用此方法汇总任意数量的日期范围,但最好将返回的行限制为ON
子句中的最大范围。只是为了展示它是如何工作的:删除
GROUP BY
和SUM()
调用,并将date_sold
添加到 SELECT 会返回以下内容:现在,当您
GROUP BY id
和SUM()
两个 Total_sold 列您就得到了结果!旧建议
在将两个不同的日期范围混合之前,您可以使用
GROUP BY
使用 table1 上的表 id 进行分组,以及SUM()
聚合函数,用于将返回的行相加。测试数据
NOW()
为2010-05-03Using Subqueries
One way to solve this would be to use subqueries.
LEFT JOIN
creates a new "result" for each match in the right table, so using two LEFT JOINs is creating more ROWS than you want. You could just sub select the value you want, but this can be slow:Result:
Using SUM(CASE ...)
This method doesn't use subqueries (and will likely be faster on larger data sets). We want to join the table_a and table_b together once, using our "biggest" date range, and then use a
SUM()
based on aCASE
to calculate the "smaller range".This returns nearly the same resultset as the subquery example:
The only difference is the
0
instead ofNULL
. You could summarize as many date ranges as you'd like using this method, but its still probably best to limit the rows returned to the largest range in theON
clause.Just to show how it works: removing the
GROUP BY
andSUM()
calls, and addingdate_sold
to the SELECT returns this:Now when you
GROUP BY id
, andSUM()
the two total_sold columns you have your results!Old Advice
Before you brought the two different date ranges into the mix, you could use
GROUP BY
to group using the table id on table1, and theSUM()
aggregate function to add up the rows returned.The test data
NOW()
is 2010-05-03