有张树形表
id   int  not null  自增
parentid int null  父节点
name varchar(123)  名称数据如下
ID     parentid   name
1      null       中国
2      null       阿联酋
3        1        浙江
4        1        中国香港select a.id,a.name,b.name as parentname 
from T_Area a,T_Area b
where a.parentid = b.id  and a.name = '浙江'
正确的
select a.id,a.name,b.name as parentname 
from T_Area a,T_Area b
where a.parentid = b.id  and a.name = '中国'
就没有数据了我知道Where条件a.parentid = b.id有错误,麻烦大虾帮忙改下,新手感激不尽。。

解决方案 »

  1.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([id] int,[col1] varchar(8),[col2] int)
    insert [tb]
    select 1,'河北省',0 union all
    select 2,'邢台市',1 union all
    select 3,'石家庄市',1 union all
    select 4,'张家口市',1 union all
    select 5,'南宫',2 union all
    select 6,'坝上',4 union all
    select 7,'任县',2 union all
    select 8,'清河',2 union all
    select 9,'河南省',0 union all
    select 10,'新乡市',9 union all
    select 11,'aaa',10 union all
    select 12,'bbb',10;with t as(
    select * from [tb] where col1='河北省'
    union all
    select a.* from [tb] a  ,t where a.col2=t.id)
    select * from t
    /*
    id          col1     col2
    ----------- -------- -----------
    1           河北省      0
    2           邢台市      1
    3           石家庄市     1
    4           张家口市     1
    6           坝上       4
    5           南宫       2
    7           任县       2
    8           清河       2(8 行受影响)
    */
      

  2.   

    select a.id,a.name,b.name as parentname 
    from T_Area a,T_Area b 
    where (a.parentid = b.id or a.parentid is null) 
    and (a.name = '中国') 
      

  3.   

    用临时表
    if OBJECTPROPERTY(object_id('tb'),'isusertable')<>0 
      drop table tb
    create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000))
    insert tb
    select '0001',null,'云南省'
    union all select '0002','0001','昆明市'
    union all select '0003','0001','昭通市'
    union all select '0009','0001','大理市'
    union all select '0008',null,'四川省'
    union all select '0004',null,'贵州省'
    union all select '0005','0002','五华区'
    union all select '0007','0002','水富县'
    union all select '0006','0005','西园路192号'
    union all select '0010','0006','金色梧桐3-702'
    union all select '0011','0010','科技有限公司'
    union all select '0015','0007','两碗乡'
    union all select '0013','0015','两碗村'
    union all select '0012','0013','某跨国集团董事长'
    union all select '0014','0008','成都市'   declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
      declare @level int
      set @level=0
      insert @level_tt(ybh,ebh,level)
      select ybh,ybh,@level from tb where ebh is null
      while @@ROWCOUNT>0
      begin 
              set @level=@level+1
              insert @level_tt(ybh,ebh,level)
              select a.ybh,b.ebh+a.ybh,@level
                from tb a,@level_tt b
                where a.ebh=b.ybh and b.level=@level-1
     end
    select space(b.level*2)+'----'+a.beizhu,a.*,b.*
      from tb a,@level_tt b
      where a.ybh=b.ybh
      order by b.ebh
      
      
    /*
    (无列名) ybh ebh beizhu ybh ebh level
    ----云南省 0001 NULL 云南省 0001 0001 0
      ----昆明市 0002 0001 昆明市 0002 00010002 1
        ----五华区 0005 0002 五华区 0005 000100020005 2
          ----西园路192号 0006 0005 西园路192号 0006 0001000200050006 3
            ----金色梧桐3-702 0010 0006 金色梧桐3-702 0010 00010002000500060010 4
              ----科技有限公司 0011 0010 科技有限公司 0011 000100020005000600100011 5
        ----水富县 0007 0002 水富县 0007 000100020007 2
          ----两碗乡 0015 0007 两碗乡 0015 0001000200070015 3
            ----两碗村 0013 0015 两碗村 0013 00010002000700150013 4
              ----某跨国集团董事长 0012 0013 某跨国集团董事长 0012 000100020007001500130012 5
      ----昭通市 0003 0001 昭通市 0003 00010003 1
      ----大理市 0009 0001 大理市 0009 00010009 1
    ----贵州省 0004 NULL 贵州省 0004 0004 0
    ----四川省 0008 NULL 四川省 0008 0008 0
      ----成都市 0014 0008 成都市 0014 00080014 1
      */
      

  4.   


    select a.id,a.name,b.name as parentname 
    from T_Area a,T_Area b 
    where a.parentid = b.id  and a.name = '中国' and a.parentid is not null
      

  5.   

    以上两句都不对,我要的结果是一条数据  也就是id     parentid    name
    1       null       中国如果
    select a.id,a.name,b.name as parentname 
    from T_Area a,T_Area b 
    where a.parentid = b.id  and a.name = '浙江'id   name   parentname
    3    浙江    中国
      

  6.   

    如果 
    select a.id,a.name,b.name as parentname 
    from T_Area a,T_Area b 
    where a.parentid = b.id  and a.name = '浙江' id  name  parentname 
    3    浙江    中国我想要输入中国的时候也要有数据显示
      

  7.   


    if object_id('T_Area ') is not null drop table T_Area 
    GO
    create table T_Area ([ID] int, parentid int,  name varchar(20))
    insert T_Area select
    1  ,    null    ,  '中国' union all select
    2   ,   null   ,   '阿联酋' union all select
    3    ,    1  ,      '浙江' union all select
    4     ,   1 ,       '中国香港' 
    select a.id,a.name,b.name as parentname 
    from T_Area a,T_Area b 
    where a.parentid = b.id  and a.name = '中国' and a.parentid is not null
    union all 
    select * from T_Area  where name = '中国' and parentid is  null
    /*
    id          name        parentname
    ----------- ----------- --------------------
    1           NULL        中国(1 行受影响)
    */
    select a.id,a.name,b.name as parentname 
    from T_Area a,T_Area b 
    where a.parentid = b.id  and a.name = '浙江' ----------- -------------------- --------------------
    3           浙江                   中国(1 行受影响)
      

  8.   

    能不能用一条SQL 实现输入不同 区域  都要有数据显示啊
      

  9.   


    if object_id('T_Area ') is not null drop table T_Area 
    GO
    create table T_Area ([ID] int, parentid int,  name varchar(20))
    insert T_Area select
    1  ,    null    ,  '中国' union all select
    2   ,   null   ,   '阿联酋' union all select
    3    ,    1  ,      '浙江' union all select
    4     ,   1 ,       '中国香港' declare @name varchar(20)
    set @name='中国'if exists(select 1 from T_Area  where name =@name and parentid is  null )
    select id, parentid,name from   T_Area where name =@name
    else
    select a.id,a.name,b.name as parentname 
    from T_Area a,T_Area b 
    where a.parentid = b.id  and a.name =@name/*
    id          parentid    name
    ----------- ----------- --------------------
    1           NULL        中国(1 行受影响)
    */set @name='浙江'if exists(select 1 from T_Area  where name =@name and parentid is  null )
    select id, parentid,name from   T_Area where name =@name
    else
    select a.id,a.name,b.name as parentname 
    from T_Area a,T_Area b 
    where a.parentid = b.id  and a.name =@name
    /*id          name                 parentname
    ----------- -------------------- --------------------
    3           浙江                   中国(1 行受影响)
    */
      

  10.   

    谢谢  大家帮忙 ,我采纳
    js_szy 
    (华夏小卒) 
    的方案了