有一个表area
表三个字段
id                 name              parent1                  东亚               null
2                  西亚               null
3                  中国               1
4                  韩国               1
5                  伊朗               2
.......................................就是这样子拉我要查询出如下格式东亚            中国
东亚            韩国
西亚            伊朗
。请问,该怎么写呢?谢谢

解决方案 »

  1.   

    select a.name , b.name from
    (select * from area where parent is null) a,
    (select * from area where parent is not null) b
    where a.id = b.parent
      

  2.   

    Select a.name,b.name from area a
     Inner Join area b on a.id=b.parent
     where a.parent is null and b.parent is not null
      

  3.   

    create table tb(id int,name varchar(10),parent int)
    insert into tb values(1,'东亚',               null)
    insert into tb values(2,'西亚',               null)
    insert into tb values(3,'中国',               1)
    insert into tb values(4,'韩国',               1)
    insert into tb values(5,'伊朗',               2)
    goselect a.name , b.name from
    (select * from tb where parent is null) a,
    (select * from tb where parent is not null) b
    where a.id = b.parentdrop table tb/*
    name       name       
    ---------- ---------- 
    东亚         中国
    东亚         韩国
    西亚         伊朗(所影响的行数为 3 行)*/
      

  4.   

    declare @a table (id int,name varchar(10),parent int)
    insert into @a values(1,'东亚',               null)
    insert into @a values(2,'西亚',               null)
    insert into @a values(3,'中国',               1)
    insert into @a values(4,'韩国',               1)
    insert into @a values(5,'伊朗',               2)select a.name,b.name from @a a,@a b where b.parent=a.id
    /*(所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)name       name       
    ---------- ---------- 
    东亚         中国
    东亚         韩国
    西亚         伊朗(所影响的行数为 3 行)
    */