SELECT taw.user_id AS taw_user_id, COALESCE(SUM(tai.bid), 0) AS tai_bid, taw.win AS taw_win
FROM tb_auction_winners taw JOIN
tb_aukciono_istorija tai
ON taw.id = tai.aukciono_id
WHERE tai.user_id = 206 AND taw.user_id = 206
GROUP BY aukciono_id, taw.user_id;
我的问题是我无法获得我想要的结果(使用inspect元素更改):
它应该对tai_bid
相同的所有taw_win
和taw_user_id
值求和。我已阅读SQL query to sum the data和How to take sum of column with same id in SQL?个帖子,但它没有帮助。使用GROUP BY aukciono_id, taw_user_id
一切都应该没问题,但它不起作用。我想这是一个非常简单的修复,但我找不到有什么问题:(请帮帮我。
SQL FIDDLE:http://sqlfiddle.com/#!9/6945a/1
答案 0 :(得分:3)
如果我做对了:你只想用taw_user_id求和 - 这就是GROUP BY子句中的唯一一列
SELECT taw.user_id AS taw_user_id
,SUM(taw.win) AS taw_win
,SUM(tai.bid) AS tai_bid
FROM tb_auction_winners taw
INNER JOIN
( SELECT aukciono_id, user_id, SUM(bid) as bid
FROM tb_aukciono_istorija
group by aukciono_id, user_id
) tai
ON taw.id = tai.aukciono_id
AND taw.user_id = tai.user_id
WHERE taw.user_id = 206
GROUP BY taw.user_id
答案 1 :(得分:2)
检查此查询
select taw_user_id,sum(taw_win) as taw_win,sum(tai_bid) as tai_bid from
(
select taw.user_id AS taw_user_id,sum(win) as taw_win ,(select sum(bid) from tb_aukciono_istorija
where user_id=taw.user_id and taw.id =aukciono_id ) AS tai_bid from tb_auction_winners taw where user_id=206
group by taw.id) temp