select author,max(pid) as maxpid from users
where tid =3 group by tid;
表结构
--------------------------------------------------------
pid    tid(部门id)    author(用户名)    
--------------------------------------------------------
1         3               张三
2         3               李四
3         3               王五
4         3               赵六
--------------------------------------------------------问题:我取出来的结果  4   张三
为什么不是4 赵六呢,请解释一下,谢谢

解决方案 »

  1.   

    select a.* from tt a inner join
    (select tid,max(pid) as ma from tt group by tid) b
    on a.tid=b.tid and a.pid=b.ma
      

  2.   

    --你的查询有点毛病,你按下边的查询再试试.select * from users a where tid=3 and pid in (select max(pid) from users where tid=a.tid);
      

  3.   

    http://blog.chinaunix.net/u/29134/showart_411484.html