如题: 
给出表A (ID,NAME,CODE) 
ID    NAME , CODE 
1      1      3 
2      1      4 
3      2      1 
4      2      2 
查询得到,每个对应的Name中ID最大的那条记录; 即: 
ID    NAME , CODE 
2      1      4 
4      2      2 

解决方案 »

  1.   

    select * 
    from A t
    where not exists(select * from A where name=t.name and code>t.code)
      

  2.   

    select a.*
    from a inner join (select name,max(id) as mid from a) b on a.id=b.id
      

  3.   


    提示出错了,为啥?我的MYSQL版本为:4.02 与版本有关吗?
      

  4.   

    #1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'exists (select * from jxc_commglide  where  fd_coge_id>t.fd_cog 
      

  5.   

    select ID,NAME,CODE from ( select * From a order by NAME, ID desc ) x group by NAME;