[英]how to use group by with inner join
I have three tables customer , bargains and installment . 我有三张桌子的顾客 , 讨价还价和分期付款 。
I want to select name from customer table and sum of total price from bargains table and sum of payment amount from installment table according to each customer and at the end subtract the sum of total price from sum of payment amount. 我想根据每个客户,并在年底从支付金额的总和减去总价格的总和,从分期付款表中选择从客户表和讨价还价表和支付金额的总和价格的总和名 。
so the result should be a list that contain name of all customers and their debt. 因此结果应为包含所有客户名称及其债务的列表。
I try the following command but it return wrong values it looks like they are summed twice or maybe more. 我尝试以下命令,但是它返回错误的值,看起来它们被加总了两次甚至更多。
select c.name, b.total_price, i.payment_amount
from customer as c
inner join (select sell_or_buy, sum(total_price) as total_price from bargains group by C_ID ) as b on (b.sell_or_buy = 'خرید')
inner join (select trade_type, sum(payment_amount) as payment_amount from installment group by C_ID ) as i on (i.trade_type = 'پرداخت')
group by c.C_ID
because I thought query sum the total price for each record of installment table and sum the payment amount for each record of bargains table I try to use select in inner joins to make each of them distinct but it doesn't work so I couldn't even subtract the results. 因为我认为查询求和了分期付款表的每条记录的总价格,并总结了讨价还价表的每条记录的支付金额,所以我尝试在内部联接中使用select来使每个联接与众不同,但这是行不通的,所以无法甚至减去结果。
I also try the following query 我也尝试以下查询
select
(select sum(payment_amount) as payment_amount from installment where trade_type = 'پرداخت'group by C_ID)-
(select sum(total_price) as total_price from bargains where sell_or_buy = 'خرید' group by C_ID)as result
which display and error which say query return more than one row. 其中显示和错误表示查询返回多行。
when I use only one select from above query the result is OK but when I try to combine the its not working. 当我仅从上面的查询中使用一个选择时,结果是确定的,但是当我尝试将其组合不起作用时。
There is no need to use subquery to do this, just join the three tables and do aggregation, try following: 无需使用子查询来执行此操作,只需将三个表联接并进行聚合,请尝试以下操作:
select
c.name,
sum(b.total_price) as total_price,
sum(i.payment_amount) as payment_amount
from customer as c
inner join bargains b on c.c_id = b.c_id and b.sell_or_buy = 'خرید'
inner join installment i on c.c_id = i.c_id and i.trade_type = 'پرداخت'
group by c.c_id -- ,c.name
and like @Tim Biegeleisen said, if the sql_mode
of your mysql db contains ONLY_FULL_GROUP_BY
, you should also add c.name
in group by
clause. 和喜欢@Tim Biegeleisen表示,如果sql_mode
你的MySQL数据库包含ONLY_FULL_GROUP_BY
,你也应该补充c.name
在group by
子句。 and use this query to check sql_mode
: 并使用此查询来检查sql_mode
:
show variables like 'sql_mode';
Your original query seems to be missing the correct join conditions. 您的原始查询似乎缺少正确的联接条件。 Try this instead: 尝试以下方法:
select c.name,
b.total_price,
i.payment_amount
from customer as c
inner join
(
select C_ID, sum(total_price) as total_price
from bargains
where sell_or_buy = 'خرید'
group by C_ID
) as b
on c.C_ID = b.C_ID
inner join
(
select C_ID, sum(payment_amount) as payment_amount
from installment
where trade_type = 'پرداخت'
group by C_ID
) as i
on c.C_ID = i.C_ID
If you want to compute the difference between the total price and payment amount, then just use this SELECT
: 如果要计算总价和付款额之间的差额,则只需使用以下SELECT
:
select c.name,
b.total_price,
i.payment_amount,
b.total_price - i.payment_amount AS diff
Can you give me an actual screen shot of each of the tables with a few entries in them? 您能给我一张每张桌子的实际屏幕截图,其中有几个条目吗?
You're probably getting duplicate rows because of this line: inner join (select trade_type, sum(payment_amount) as payment_amount from installment group by C_ID ) 您可能会因为此行而获得重复的行:内部联接(从C_ID从分期付款组中选择trade_type,sum(payment_amount)作为payment_amount)
basically, you probably shouldn't use the sum() function while also selecting values from any other field. 基本上,您可能不应该使用sum()函数,同时还要从任何其他字段中选择值。 Here's why: sum(payment_amount) should return 1 row correct? 原因如下:sum(payment_amount)应该返回正确的1行吗? But right next to it you've got "select trade_type" which probably is going to return multiple rows. 但是在它旁边,您有“ select trade_type”,它可能会返回多行。
Also, what language is this? 还有,这是什么语言? MySQL? MySQL的? SQL? SQL吗?
Subquery: 子查询:
SELECT c.Id AS CustomerId, c.Name AS CustomerName, m.Name AS ManagerName, o.SumAmmount
FROM Customers AS c
INNER JOIN Managers AS m ON c.ManagerId = m.Id
INNER JOIN
(SELECT SUM(Amount) AS SumAmmount, o.CustomerId
FROM Orders AS o
WHERE Date >= '2017-01-01'
GROUP BY CustomerId
HAVING SUM(Amount) >= 10000) AS o ON c.Id = o.CustomerId
No Subquery: 没有子查询:
SELECT c.Id AS CustomerId, c.Name AS CustomerName,
m.Name AS ManagerName, SUM(o.Amount) AS SumAmmount
FROM Customers AS c
INNER JOIN Managers AS m ON c.ManagerId = m.Id
INNER JOIN Orders AS o ON c.Id = o.CustomerId
WHERE o.Date >= '2017-01-01'
GROUP BY c.Id, c.Name, m.Name
HAVING SUM(Amount) >= 10000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.