繁体   English   中英

可能高级的SQL查询3个表的UNION

[英]Possibly advanced SQL query UNION of 3 Tables

目标。 进行查询以显示产品名称(产品),产品类型(ProductTypes)和每种产品的销售总数(销售)

这是我的桌子: 在此处输入图片说明

我确实很难弄清楚我该怎么做。 我正在尝试做一个UNION和其他一些事情,但是无法使其正常工作。 我可以通过使用此SELECT ProductID, count(*) as NumSales from Sales group by ProductID来获得销售总数,以SELECT ProductID, count(*) as NumSales from Sales group by ProductID但实际上很难解决其余问题并正确格式化它。 任何帮助,将不胜感激。

编辑: Select Products.ProductName, ProductTypes.ProductType From Products INNER JOIN ProductTypes ON Products.ProductTypeID=ProductTypes.ProductTypeID我现在要显示此信息,只需要以某种方式加入销售数量即可。

在此处输入图片说明

尝试:

select prod.ProductName, ptyp.ProductType, count(SaleID) count_sale
from Products prod
join ProductTypes ptyp on ( ptyp.ProductTypeID= prod.ProductTypeID)
join Sales sal on ( sal.ProductID = prod.ProductID)

group by prod.ProductName, ptyp.ProductType

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM