SELECT R.* FROM trans_flow R, (SELECT order_no, MAX(status_time) AS status_time FROM trans_flow GROUP BY order_no) S WHERE R.order_no = S.order_no AND R.status_time = S.status_time ORDER BY R.status_time DESC SELECT a.order_no,a.mem_no,c.card_no,c.card_holder,c.bank_name,c.branch_bank_name,a.flag, a.income_type_txt,a.amt,a.create_time FROM account_log a JOIN (SELECT b.*,MAX(id) FROM account b GROUP BY b.mem_no) c ON DATE_ADD(a.create_time, INTERVAL 7 DAY) < NOW() AND a.`mem_no` = c.mem_no ORDER BY a.id DESC
解决思路:
结构如下:
type name guest date
1 aa sa 20131101
1 bb dsf 20131102
1 aa wew 20131103
我想按照name分组,按照date降序排列,希望的结果如下:
type name guest date
1 aa wew 20131103
1 bb dsf 20131102
select R.* from tab R, (select name, max(date) from tab group by name) S
where R.name=S.name and R.date=S.date
order by R.date desc