码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2014-09-11 19:01:02      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   ar   div   sp   log   on   

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

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

标签:des   style   blog   color   ar   div   sp   log   on   

原文地址:http://www.cnblogs.com/simpledev/p/3966975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!