例一个表table1有这么几个字段bianhao mingcheng shangjjgbh shangjijgmc
有2条记录                       001   管理部    
                                002   管理餐饮部      001     管理部
记录2中的管理部是通过shangjijgbh=bianhao查出来的 怎么判断是否有shangjijgbh呢
如果想得到上面情况2条记录 SQL 该怎么写啊 

解决方案 »

  1.   

    if object_id('tb') is not null
    drop table tb
    go
    create table tb(id int,name varchar(50),parentid int)
    insert into tb select 1,'安徽省',0
    insert into tb select 2,'湖南省',0
    insert into tb select 3,'合肥市',1
    insert into tb select 4,'巢湖市',1
    insert into tb select 5,'肥西县',3
    insert into tb select 6,'肥东县',3
    insert into tb select 7,'肥东乡',6
    insert into tb select 8,'肥东村',7
    insert into tb select 9,'湖南乡',2
    insert into tb select 10,'湖南镇',9
    gowith dom 
    as
    (select * from tb where parentid=3 
    union all
    select a.* from tb a inner join dom b on a.parentid=b.id)select * from dom
      

  2.   


    select distinct a.bianhao,a.mingcheng,a.shangjjgbh,b.shangjijgmc 
    from table1 a,table1 b
    where a.bianhao=b.shangjjgbh
      

  3.   

    查询有shangjijgbh的记录:select * from table1 where shangjijgbh is not null
      

  4.   

    SQL Server2005高端培训视频,循序渐进,历时半年通过3大分系列的46次课程,让你完全掌握SQL Server2005.
    同Visual Studio 2005以及Microsoft .NET集成的开发特性,通过实例由浅入深的帮助您掌握这些看似难懂的技术。
    http://auction1.taobao.com/auction/item_detail-0db2-512881e1a3aab7111ccf190b0068536d.jhtml