假设我试图从本月开始寻找商店出售的十大冰淇淋口味。然而,我目前的查询只给出了7月份的十大口味
select *
from buy_history
where date > '2012-07-01 00:00:00'
group by flavor
order by max(purchase_count) desc
limit 10;
(显然每毫秒可以购买多个冰淇淋。)
表格看起来像
buy_history(id, flavor, date, purchase_count)
答案 0 :(得分:0)
如何使用以下日期条件
WHERE month(date)=3
答案 1 :(得分:0)
select flavor, sum(purchase_count) as purchase_count_total
from buy_history
where date >= CONCAT(YEAR(CURDATE()), '-' MONTH(CURDATE()), '-1')
group by flavor
order by sum(purchase_count) desc
limit 10;
答案 2 :(得分:0)
只有这样才会出现。进行手动计数(或将其输入电子表格并进行计算和验证)。
此处的问题是您要按特定列(group by flavor
)对结果进行分组,并且您的结果中包含的其他列既不包含在group by
子句中,也不包含在聚合函数中。 MySQL因此不知道如何显示这些列(服务器选择显示哪一列)。
你应该这样做(*粗略地):
select flavor, sum(purchase_count) as total_sold
from buy_history
where date > '2012-07-01 00:00:00'
group by flavor
order by total_sold desc
limit 10;