有min的mysql组

前端之家收集整理的这篇文章主要介绍了有min的mysql组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是表格数据(一小块)基本上我想查询按照accoutn编号分组时具有最小original_date_ctr的行.@H_404_2@

我尝试过使用HAVING(MIN()),其中= Min()和其他方式没有运气.@H_404_2@

这里的正确结果会给我id_ctr 688,1204和1209@H_404_2@

@H_404_2@

id_ctr  account_number_cus  original_date_ctr   mrc_ctr  
------  ------------------  -----------------  ----------
   688               20062  2008-05-17             138.97
  1204              151604  2006-08-10           42000.00
  1209              151609  2006-06-29             968.68
  1367               20062  2011-10-27             207.88
  1434              151609  2009-09-10            1469.62
  1524              151604  2009-09-01           36999.99
  1585              151609  2012-05-31            1683.88
最佳答案
您可以通过以下方式执行此操作:@H_404_2@

@H_404_2@

select t1.id_ctr,t1.account_number_cus,t1.original_date_ctr,t1.mrc_ctr  
from yourtable t1
inner join
(
    select min(original_date_ctr) as mindate,account_number_cus
    from yourtable
    group by account_number_cus
) t2
    on t1.account_number_cus = t2.account_number_cus
    and t1.original_date_ctr = t2.mindate

SQL Fiddle with Demo

猜你在找的MySQL相关文章