22,209
社区成员
发帖
与我相关
我的任务
分享
create table tab6
(
tid int,
tname varchar(10),
topid int
)
insert into tab6
select 1,'aaa',0 union all
select 2,'bbb',0 union all
select 3,'ccc',1 union all
select 4,'ddd',1 union all
select 5,'eee',2 union all
select 6,'fff',2
select * from tab6
select a.tid,a.tname,b.tname
from tab6 a,tab6 b
where a.topid=b.tid
tid tname tname
----------- ---------- ----------
3 ccc aaa
4 ddd aaa
5 eee bbb
6 fff bbb
(4 row(s) affected)
select id,name,c.name as topname
from tb a left join tb b on a.topid=b.id
where c.name is not null
select b.id,b.name,a.name as topname from tname a,tname b where a.id=b.topid order by b.id