MySQL查询 - 大多数用户购买的商品

时间:2012-02-21 19:36:43

标签: mysql

我在这里苦苦挣扎 我有两张桌子:
  item(IID, description, category, price)
  buys(UID,IID,Price)

查询是查找大多数用户购买的商品。 我试过这个:

SELECT Distinct Description, Count(buys.UID) AS TimesBought From item,buys
Where item.IId=buys.IID
Group by Description Order by TimesBought;

返回所有类别以及此类别中的商品购买次数。

请帮忙

1 个答案:

答案 0 :(得分:2)

SELECT item.IID, Count(item.IID) AS TimesBought
From item inner join buys
on item.IId = buys.IID
Group by item.IId Order by TimesBought