mysql - join first and last records by group type?
问题 With a table like: idx type dat 0 a foo1 1 b foo2 2 c foo3 3 a foo4 4 b foo5 5 c foo6 6 a foo7 7 b foo8 8 c foo9 How can i get the first and last data of each type: for example: a foo1 foo7 b foo2 foo8 c foo3 foo9 I have tried this query, but its way to slow, even with indexes on idx and type: select mins.type, mins.dat, mytable.dat from mytable --get the maximums inner join (select max(idx) as maxidx from mytable group by type) as a on a.maxidx = mytable.idx --join the maximums to the