Group By / Order By mysql

时间:2012-10-24 15:15:29

标签: mysql

我有一张包含客户ID和购买日期的表格,我按客户分组,但我希望上次购买日期不显示第一个购买日期

我试过

GROUP BY客户

ORDER BY已购买DESC

3 个答案:

答案 0 :(得分:2)

您需要哪个购买日期?如果上次购买日期,那么查询应该是这样的:

SELECT *, max(purchasedate) FROM table1 GROUP BY customer

答案 1 :(得分:2)

您需要发布您的实际查询以及我们的上下文,以便更好地为您提供帮助。

好像你错过了聚合。你可以试试这个:

SELECT customer, MAX(purchasedate)
FROM sparkles
GROUP BY customer

答案 2 :(得分:2)

如果没有看到您的实际查询,您可以使用与此类似的内容:

select t1.customerid, t1.purchasedate, t1.othercol1, t1.othercol2, t1.othercol3
from yourtable t1
inner join
(
    select max(purchasedate) purchasedate, customerid
    from yourtable
    group by customerid
) t2
    on t1.customerid = t2.customerid
    and t1.purchasedate = t2.purchasedate
order by t1.purchasedate desc

或者您可以将max()汇总功能应用于purchasedate