表里面存在这样的数据
id pid content
1   1   sha
2   1   kkd
3   1   sdf
4   2   sdf
5   2   111
6   3   233
7   3   sdf
8   4   sda
根据PID分组 并按照升序排列,我只想得到分组后的每个分组的第一条数据,请问在mysql环境下sql语句怎么写。
在线等...各位帮帮忙。 

解决方案 »

  1.   

    select * from 表 a where not exists(select * from 表 where pid=a.pid and id<a.id)/**
    id          pid         content              
    ----------- ----------- -------------------- 
    1           1           sha
    4           2           sdf
    6           3           233
    8           4           sda
    **/
      

  2.   

    http://yueliangdao0608.blog.51cto.com/397025/81270
      

  3.   

    假设取ID为最小:
    select a.* from tt a
    inner join (select pid,min(id) as ma from tt group by pid ) b
    on a.pid=b.pid and a.id=b.ma