mysql去除重复查询的SQL语句基本思路

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。