Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 查询订购所有类型产品的门店_Sql_Postgresql - Fatal编程技术网

Sql 查询订购所有类型产品的门店

Sql 查询订购所有类型产品的门店,sql,postgresql,Sql,Postgresql,在我的数据库中: 产品类型:type(PK)|说明 产品:id(PK)| title | type(FK)| price 订单列表:(productid(FK)、orderid(FK))(PK)|数量 订单:orderid(主键)|日期| shopid(FK) 店铺:shopid(主键)|店名|地址|电话 我必须如何创建查询以了解哪些商店订购所有类型的产品。使用分组方式和拥有 Select s.shopid, s.shoptitle From shop s join order o on s

在我的数据库中:

产品类型:
type(PK)|说明

产品:
id(PK)| title | type(FK)| price

订单列表:
(productid(FK)、orderid(FK))(PK)|数量

订单:
orderid(主键)|日期| shopid(FK)

店铺:
shopid(主键)|店名|地址|电话


我必须如何创建查询以了解哪些商店订购所有类型的产品。

使用
分组方式和
拥有

Select s.shopid, s.shoptitle
  From shop s join order o on s.shop_id = o.shop_id
  Join orderlist ol on ol.order_id = o.order_id
  Join product p on p.id = ol.product_id
Group by s.shopid, s.shoptitle
Having count(distinct p.type) = (select count(1) from producttype)

如果你期望
count(1)
count(*)快,那你就错了-事实上它实际上慢了一点。谢谢你,idk,我可以在By组中使用多个列。这只是使用1的练习。我认为1和*将提供相同的性能。请注意,
s.shoptitle
groupby
子句中是多余的,因为
s.shopid
是主键,
s.shoptitle
完全依赖于它<代码>按s.shopid分组就足够了。但我用它来提供更多信息,这取决于OP是否愿意使用它