persons:
id name 
1 小一
2 小二
3 小三hetong:
id ht_no
1 14
1 15
1 16
2 17我要写一个语句,查询出每个人对应的最新的合同。最新是以ht_no来标识的,也就是最大值。
结果应该是:
id       hetong
1        16
2        17谢谢大家。

解决方案 »

  1.   

    select a.id,b.hetong
    from persons a
    left join (select id,max(ht_no) as ht_no from hetong group by id)b on a.id=b.id
      

  2.   


    select persoms.id,T.ht_no  from persoms
    join (select id,max(ht_no) as ht_no from hetong group by ID) T
    on persoms.id=td.id
      

  3.   


    select a.id,b.ht_no 
    from persoms a join (select id,max(ht_no) ht_no 
                         from hetong 
                         group by id) b on a.id=b.id
      

  4.   


    select id, ht_no as [hetong]
    from hetong
    group by id
      

  5.   

    sorry!select id, max(ht_no) as [hetong]
    from hetong
    group by id