表AAA有三个字段分别为:A(id) B(父id) C(内容) 默认记录如下: 
1 0 A
2 0 B
3 1 C
4 3 D
5 4 E
6 0 F
7 0 G
8 5 H
...请教想实现这种列表如何写SQL...
A
C
D
E
H
...

解决方案 »

  1.   

    select distinct 父 order by 父 ASC
      

  2.   

    select a.内容 
    from AAA a inner join (
    select 父id,min(id) as mid
    from AAA
    group by 父id
    ) b on a.id=b.min
      

  3.   

    select a.内容
    from AAA a 
    where not exists (select id from AAA where 父id=a.父id and id<a.id)
      

  4.   

    select a.内容
    from AAA T1
    where exists (select 1 from AAA T2 where T1.id=T2.id )