在一个名为inventory的表中,我有两个名为food和brand的列。以下是表格内容的示例:
food | brand
----------------------------
cookies | Pepperidge Farm
cookies | Pepperidge Farm
goldfish | Pepperidge Farm
cookies | Oreo
crackers | Ritz
goldfish | Pepperidge Farm
crackers | Ritz
crackers | Ritz
goldfish | Pepperidge Farm
goldfish | Pepperidge Farm
我正在尝试编写一个mysql查询,它将从表中提取三个最受欢迎的项目,品牌组合。我遇到的麻烦是该品牌可以有多个项目,但我想要计算最受欢迎的品牌和项目,而不是最受欢迎的品牌。因此,在上表中,查询将按照最热门到最不受欢迎(仅限于三个结果)的顺序返回以下内容:
goldfish | Pepperidge Farm
crackers | Ritz
cookies | Pepperidge Farm
答案 0 :(得分:0)
SELECT COUNT(*) AS `Rows`, FOOD, BRAND
FROM `foods`
GROUP BY FOOD, BRAND
ORDER BY `Rows` DESC