简体   繁体   English

SQL从不同表中选择多个位置的总和

[英]SQL select sum with multiple where from different tables

So I have two tables in my MySQL, users and purchases. 所以我的MySQL中有两个表,即用户和购买。

User table contains Id, Name, Surname and so on. 用户表包含ID,名称,姓氏等。 Purchase table contains Id, User_id, Status and Sum payed for something. 购买表包含ID,User_id,状态和所支付的总金额。

What I need is to get select from these two tables where I would have the sum of every item each user has buyed. 我需要从这两个表中进行选择,我将获得每个用户购买的每件商品的总和。

I've tried this with table 'users': 我已经尝试使用表“用户”:

LEFT JOIN 'purchases'
ON 'users'.'id' = 'purchases'.'user_id'
SELECT SUM (sum) FROM 'users', 'purchases' WHERE 'purchases'.'status' = 'completed' AND 'purchases'.'user_id' = 'users'.'id'

But with this I get the same sum for each user, which is wrong. 但是,这样做我为每个用户获得了相同的总和,这是错误的。 What should I write instead? 我应该写些什么呢?

Would highly appreciate any possible help! 非常感谢您提供任何帮助!

To provide some actual example, lets take this one: 为了提供一些实际的例子,让我们举一个例子:

Users:
Id: 1 Name: John
Id: 2 Name: Ann
Purchases:
Id: 10 Status: completed User_id: 1 Sum: 4.00
Id: 20 Status: completed User_id: 1 Sum: 8.00
Id: 30 Status: completed User_id: 2 Sum: 50.00
Id: 40 Status: completed User_id: 2 Sum: 100.00

After running the query I should get this: 运行查询后,我应该得到这个:

 Id: 1 Sum: 12.00 
 Id: 2 Sum: 150.00

The query was made via Laravel, so I had just 该查询是通过Laravel进行的,所以我刚刚

DB::table('users')->leftJoin... ->select(DB::raw...

Original Answer 原始答案

First off, your submitted SQL was a mess, things were completely out of the conventional order, I'm not sure it would execute as presented. 首先,您提交的SQL太混乱了,一切完全超出了常规顺序,我不确定它是否会按所示执行。 That said, this should do what you want, assuming I understand your desired output correctly: 就是说,假设我正确理解了您期望的输出,这应该做您想要的:

SELECT users.id, users.name, users.surname, ROUND(SUM(purchases.sum),2) AS totalSum
FROM purchases
    LEFT JOIN users ON users.id = purchases.user_id
WHERE status='completed'
GROUP BY users.name, users.surname
ORDER BY users.id

The key aspect you were missing from your query was the GROUP BY section, I added ORDER BY for my own benefit on my fiddle, use it if you wish, or not. 您查询中缺少的关键方面是GROUP BY部分,我出于自己的利益而添加了ORDER BY ,这是我自己的目的,请根据需要使用或不使用。

Here's a fiddle I prepped for you prior to your posting of your sample data, the main answer has been given by others I see so I'm just posting this because I already did the work on it. 这是我在发布示例数据之前为您准备的小提琴,主要答案是我看到的其他人给出的,所以我之所以发布,是因为我已经在上面做过工作。 http://sqlfiddle.com/#!9/8fa2d/6/0 http://sqlfiddle.com/#!9/8fa2d/6/0

Answer 2.0 答案2.0

Fiddle is acting up so this is untested but give it a try. Fiddle正在表演,因此未经测试,请尝试一下。

SELECT users.id, users.name, users.surname, ROUND(SUM(CASE WHEN purchases.status = 'completed' THEN purchases.sum ELSE 0 END),2) AS totalSum 
FROM users 
    LEFT JOIN purchases ON users.id = purchases.user_id 
GROUP BY users.id
ORDER BY users.id

You need to group by the field you want the aggretates to be based on. 您需要按要聚集的基础分组。 I don't know your table structures but something like this should do. 我不知道您的表结构,但是应该这样做。

SELECT 
  U.UserId, U.UserName, Sum(P.AmountPaid)
FROM Users U
LEFT OUTER JOIN Purchases P ON P.UserId = U.UserId AND P.Status = 'completed'
GROUP BY
 U.UserId, U.UserName

Try this query: 试试这个查询:

SELECT 
  U.UserId, Sum(P.AmountPaid)
FROM Users U
LEFT OUTER JOIN Purchases P ON P.UserId = U.UserId 
GROUP BY
 U.UserId
SELECT columns, SUM(column to sum)
FROM (tables you want)
WHERE FILTER
GROUP BY column (for example customer ID)

You have set a GROUP BY that your database knows, which things have to get summed. 您已经设置了数据库知道的GROUP BY ,必须汇总哪些内容。

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

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