如何显示至少有10个销售的商店

时间:2019-01-27 13:28:42

标签: sql postgresql

我有一张桌子,上面有商店(编号,商店名称,地址)和另一张表销售(商店编号,产品,价格,数量)。 我如何显示至少有10个销售量的商店,

我知道这行代码不好where count(shopsid) >= 10

select id, shopname from shops
join sales on sales.shopsid = shops.id
where count(shopsid) >= 10
order by id;

我要看的是至少有10个销售的商店的ID。 而且我不知道如何使这项工作有效或正确实施。

3 个答案:

答案 0 :(得分:2)

select id, shopname from shops
join sales on sales.shopsid = shops.id
GROUP BY id, shopname
HAVING count(shopsid) >= 10
order by id;

答案 1 :(得分:0)

使用聚合函数时必须使用分组依据

    Select Id,shopname
   from shops s join sales ss on ss.shopsid=s.id
   Grop by id , shopname
   Having count(*) between 10 and 15

拥有分组过滤器,因此您必须使用where代替

答案 2 :(得分:0)

如果您只想要id个商店,则不需要join

select shopsid
from sales
group by shopsid
having count(*) >= 10;