我有3张桌子:
当我跑步时:
select
customers.name,
SUM(items.price) as "price"
from items
INNER JOIN purchases
ON items.id = purchases.item_id
INNER JOIN customers
ON purchases.customer_id = customers.id
GROUP BY customers.name
ORDER BY "price"
这是我得到的结果:
"Anne Watson" "5.00"
"Craig Scott" "11.30"
"Michael Adam" "101.29"
"Jose Salvatierra" "899.00"
"Rolf Smith" "1174.50"
但是,我想只显示那些价格超过100的行(在这种情况下为3个底部行)。我怎么能这样做?
答案 0 :(得分:2)
这是您使用HAVING
子句的地方。它与WHERE
子句类似,但在GROUP BY
之后应用。
HAVING SUM(items.price) > 100
答案 1 :(得分:1)
如果您只想要大于100的SUM(价格)包含如下的HAVING子句。
select
customers.name,
SUM(items.price) as "price"
from items
INNER JOIN purchases
ON items.id = purchases.item_id
INNER JOIN customers
ON purchases.customer_id = customers.id
GROUP BY customers.name
having sum(items.price) >100
答案 2 :(得分:0)
这是WHERE函数的解决方案
select
customers.name,
SUM(items.price) as price
from items, purchases, customers
where
items.id = purchases.item_id and purchases.customer_id = customers.id and
items.price > 100
GROUP BY customers.name
ORDER BY price;