SELECT User.username,
IFNULL(SUM(credit) - SUM(debit), 0) AS total,
( SELECT SUM(Bid.credit) - SUM(Bid.debit) AS freebids
FROM users AS User
LEFT
JOIN bids AS Bid
ON Bid.user_id = User.id
WHERE Bid.type = 2
) AS FreeBids,
( SELECT SUM(Bid.credit) - SUM(Bid.debit) AS Normalbids
FROM users AS User
LEFT
JOIN bids AS Bid
ON Bid.user_id = User.id
WHERE Bid.type = 0
OR Bid.type = 1
) AS NormalBids
FROM users AS User
LEFT
JOIN bids AS Bid
ON Bid.user_id = User.id
GROUP
BY User.id
这是我的示例表:对于用户
id username
1 user1
2 user2
3 user3
4 user4
投标
id user_id debit credit type
1 1 0 10 0
2 1 1 5 2
3 1 1 0 2
4 3 0 10 0
6 2 0 10 0
7 4 1 10 0
8 4 1 0 1
但是我在显示子查询时遇到问题(Freebids 和 Normalbids)都具有相同的值,这里是示例显示:
username total FreeBids NormalBids
user1 10 12809 965
user2 20 12809 965
user3 9 12809 965
user4 0 12809 965
我不知道我在查询中去哪儿了。有什么建议可以解决我的问题吗?感谢您的帮助。