1

我有一张物品表以及它们来自哪个供应商:tabelitems

Ttem ID - Item Title - Supplier - Date added

供应商可以提供多个项目,标题和供应商都不是唯一的。

我想显示最新添加的 56 项,并且从这 56 项中我想显示前十名供应商

所以我个人有:

select * 
from items 
order by dateadded desc 
limit 56

而对于供应商

select count(supplier) as howmany 
from items 
group by supplier 
order by howmany desc 
limit 10

我的问题是如何显示选择的56个项目的前十名供应商

谢谢你的帮助

4

2 回答 2

1

只需结合您的两个语句并将第一个语句用作子选择:

select count(*) as howmany from (
    select * from items order by dateadded desc limit 56
) top_items group by supplier order by howmany desc limit 10

更新:您最初拥有count(supplier),它应该可以工作,但不是很清楚(看起来您正在尝试计算供应商的数量)。我改为count(*)突出显示您正在计算每个供应商的行数(项目数)这一事实。

于 2012-10-08T09:55:05.843 回答
0

这将为您提供最近 56 项商品的前 10 名供应商:

SELECT COUNT(supplier) AS howmany
FROM items

WHERE item_id IN (
    SELECT t2.item_id
    FROM items t2
    ORDER BY t2.dateadded DESC
    LIMIT 56
)

GROUP BY supplier
ORDER BY howmany DESC
LIMIT 10;
于 2012-10-08T09:55:00.790 回答