5

目标

数一数市场有多少产品。

问题

查询只返回第一个结果,我想要inMarketsQuantity每个产品的结果。

场景

这是查询:

Select product.Name As productName, 
       Count(marketProducts.ProductId) As inMarketsQuantity
From products As product
Join market_products As marketProducts On product.Id = marketProducts.ProductId
Join markets As market On marketProducts.MarketId = market.Id

这是结果:

+---------------+-------------------+
| productName   | inMarketsQuantity |
+---------------+-------------------+
| Playstation 3 | 15                |
+---------------+-------------------+

这就是我所期待的:

+---------------+-------------------+
| productName   | inMarketsQuantity |
+---------------+-------------------+
| Playstation 3 | 10                |
+---------------+-------------------+
| Xbox          | 5                 |
+---------------+-------------------+

我已经尝试过的

我已经尝试过以下查询,但我收到了SQL 错误 (1064)

Select product.Name As productName, 
       Distinct(Count(marketProducts.ProductId)) As inMarketsQuantity

[...]
4

2 回答 2

1

尝试分组ProductId

Select product.Name As productName,
    Count(marketProducts.ProductId) As inMarketsQuantity
From products As product
    Join market_products As marketProducts On product.Id = marketProducts.ProductId
    Join markets As market On marketProducts.MarketId = market.Id
GROUP BY marketProducts.ProductId
于 2013-09-04T22:27:01.547 回答
0
Select product.Name As productName, 
       Count(marketProducts.ProductId) As inMarketsQuantity
From products As product
Join market_products As marketProducts On product.Id = marketProducts.ProductId
Join markets As market On marketProducts.MarketId = market.Id GROUP BY product.Name
于 2013-09-04T22:23:12.873 回答