分组依据中的第一行与最后一行 [英] 1st Row in Group By vs. Last Row
本文介绍了分组依据中的第一行与最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
想知道如何返回MySQL组中的第一行,而不是最后一行.返回一系列日期,但需要显示第一个日期.
Wondering how to return the 1st row in a MySQL group by vs. the last row. Returning a series of dates, but need the first one displayed.
select * from calendar where approved = 'pending' group by eventid;
推荐答案
使用max
的min
功能insead返回组中的第一行与最后一行.
Use the min
function insead of max
to return the first row in a group vs the last row.
select min(date) from calendar ...
如果您希望每个组都有整行,请将表本身与每个组的最小日期进行过滤:
If you want the entire row for each group, join the table with itself filtered for the min dates on each group:
select c2.* from
(select eventid, min(dt) dt from calendar
where approved = 'pending' group by eventid) c1
inner join calendar c2 on c1.eventid=c2.eventid and c1.dt=c2.dt
group by eventid;
演示: http://www.sqlize.com/6lOr55p67c
这篇关于分组依据中的第一行与最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文