我的SQL只返回一个字段,它应该为每个用户返回一个字段。
知道我哪里出错了吗?如果您需要我可以提供的其他信息,但我现在还不确定该去哪里。
这是我的SQL:
SELECT uId, uForename, SUM(biProductPrice * biQuantity) AS uTotalSpent
FROM users
LEFT JOIN orders ON uId = ordUserId
LEFT JOIN basket ON ordUserId = bUserId
LEFT JOIN basketitems ON bId = biBasketId
WHERE ordStatus BETWEEN 4 AND 50
GROUP BY uId, uForename
以u开头的任何列都属于users表。
以ord开头的任何列都属于orders表。
以b开头的任何列都属于购物篮表。
以bi开头的任何列都属于basketitems表。
编辑:
现在一切正常,除了我的SUM,只有2个字段的ordStatus在4到50之间,因此它们是唯一适用的字段,一个的biQuantity为8,biProductPrice为100,另一个字段为biQuantity为1,biProductPrice为100,为什么返回值为400?
答案 0 :(得分:2)
由用户分组,并且将为每个
返回总和SELECT users.id, users.name, SUM(biProductPrice) AS uTotalSpent
FROM users
LEFT JOIN orders ON uId = ordUserId
LEFT JOIN basket ON ordUserId = bUserId
LEFT JOIN basketitems ON bId = biBasketId
WHERE ordStatus BETWEEN 4 AND 50
group by users.uId, users.name
答案 1 :(得分:0)
SELECT users.id, users.name, SUM(biProductPrice) AS uTotalSpent
FROM users
LEFT JOIN orders ON uId = ordUserId
LEFT JOIN basket ON ordUserId = bUserId
LEFT JOIN basketitems ON bId = biBasketId
WHERE ordStatus BETWEEN 4 AND 50
group by users.uId, users.name