我指的是这个数据库:http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
我正在尝试完成以下查询: "哪个供应商提供大多数产品?"
我来到这个查询
select SupplierName, max(count)
from(
select SupplierName, count(SupplierName) as count
from
Products as p
inner join
Suppliers as s
on p.SupplierID = s.SupplierID
group by SupplierName
)
但结果只有一半。
SupplierName | max(count)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
|
Zaanse Snoepfabriek | 5
值5是正确的,但SupplierName只是内部SQL查询的最后一个值。我总是通过max(count)语句自动提供正确的SupplierName。 为什么不喜欢这样?
如果我将查询更改为此,一切正常:
select SupplierName, max(count)
from(
select SupplierName, count(SupplierName) as count
from
Products as p
inner join
Suppliers as s
on p.SupplierID = s.SupplierID
group by SupplierName
) t
where t.count =
(
select max(count)
from(
select SupplierName, count(SupplierName) as count
from
Products as p
inner join
Suppliers as s
on p.SupplierID = s.SupplierID
group by SupplierName
)
)
但是这样我创建了一个全新的表来获取最大值然后将其与t.count进行比较。 如果数据库是1,000,000+个记录,事情就会变得严重。
这个问题有更优雅的方法:
计算一些东西,然后得到它的最大值,并获得相关的ID /名称。
答案 0 :(得分:1)
你能试试吗?我认为它正在做正确的工作,因为你从表中查询MAX(COUNT),我想它会返回计数最多的值。
select S.SupplierName, R.count
from Suppliers S
LEFT JOIN (
select SupplierName, count(SupplierName) as count
from
Products as p
inner join
Suppliers as s
on p.SupplierID = s.SupplierID
group by SupplierName
) R
ON R.SupplierName = S.SupplierName
ORDER BY S.SupplierName
但是,对于您的原始问题“哪个产品最买?”,我会使用此查询:
SELECT p.productname, od.ProductId, SUM(Quantity)
FROM [OrderDetails] od
JOIN Products P
ON od.ProductId = P.productId
GROUP BY od.ProductId, P.Productname
ORDER BY SUM(Quantity) DESC
供应商提供材料,与销售商品无关。顾客就是购买者。然而,问题只关注哪种类型的产品以最高的数量销售 - 这可以从订单详细信息表中找到,如果您将其与产品表一起加入,您可以命名所有销售的产品及其销售数量。
<强>更新强>
对于提供最多产品的供应商,我会使用:
SELECT P.SupplierId, S.SupplierName, COUNT(P.SupplierID)
FROM Products P
JOIN Suppliers S
ON S.SupplierId = P.SupplierId
GROUP BY P.SupplierId, S.SupplierName
ORDER BY COUNT(P.SupplierId) DESC
LIMIT 1
LIMIT 1(在MSSQL中略有不同 - SELECT TOP 1 ...
)将确保您获得最佳结果。但是,如果供应商与供应商品的数量相同,那么您必须将最大数量存储在单独的变量或查询中。正如在W3SCHOOLS托管的网站中使用变量不是要走的路,这是一个可以做同样工作的查询:
SELECT S.SupplierName, t.TotalCount
FROM Suppliers S
JOIN
( SELECT P.SupplierId, S.SupplierName, COUNT(P.SupplierID) as TotalCount
FROM Products P
JOIN Suppliers S
ON S.SupplierId = P.SupplierId
GROUP BY P.SupplierId, S.SupplierName) t
ON t.SupplierId = S.SupplierId
JOIN
( SELECT SupplierId, SupplierName, MAX(TotalCount) as Maximum
FROM
(SELECT P.SupplierId, S.SupplierName, COUNT(P.SupplierID) as TotalCount
FROM Products P
JOIN Suppliers S
ON S.SupplierId = P.SupplierId
GROUP BY P.SupplierId, S.SupplierName) t2
) t3
ON t3.Maximum = t.TotalCount
WHERE t.TotalCount = t3.Maximum
ORDER BY t.TotalCount DESC
这与您在原始问题中发布的基本相同,所以它没有多大帮助......:\如果它是在“现实生活”的例子中,您可以引入一个有助于整理的变量查询了一下。基本上你分配我在上面第二个连接中使用的查询结果,你只需在where子句中引用它。
这里有更多变量:http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
SET @max = (SELECT MAX(TotalCount) as Maximum
FROM
( SELECT P.SupplierId, S.SupplierName, COUNT(P.SupplierID) as TotalCount
FROM Products P
JOIN Suppliers S
ON S.SupplierId = P.SupplierId
GROUP BY P.SupplierId, S.SupplierName) t);
SELECT S.SupplierName, t.TotalCount
FROM Suppliers S
JOIN
( SELECT P.SupplierId, S.SupplierName, COUNT(P.SupplierID) as TotalCount
FROM Products P
JOIN Suppliers S
ON S.SupplierId = P.SupplierId
GROUP BY P.SupplierId, S.SupplierName) t
ON t.SupplierId = S.SupplierId
WHERE t.TotalCount = @max
ORDER BY t.TotalCount DESC