变压器是1级菜单,电容器是2级菜单(是变压器的子菜单,)。1级菜单的flag为0
 id   name    leave   flag
1    电容器1   2       5
2    电容器2   2       5
3    电容器3   2       5
4    电容器4   2       5
5    变压器1   1       0求sql语句
结果如下name    xingzhi
电容器1  变压器1
电容器2  变压器1
电容器3  变压器1
电容器4  变压器1

解决方案 »

  1.   


    select *
    from tb a cross join (select name from tb where leave = 1)b
    where a.leave = 2
      

  2.   

    select *
    from tb a, (select name from tb where leave = 1)b
    where a.leave = 2
      

  3.   

     id name leave flag
    1 电容器1 2 5
    2 电容器2 2 5
    3 电容器3 2 5
    4 电容器4 2 6
    5 变压器1 1 0
    6 变压器2 1 0
    7 电容器5 2 6
    求sql语句
    结果如下name xingzhi
    电容器1 变压器1
    电容器2 变压器1
    电容器3 变压器1
    电容器4 变压器2
    电容器5 变压器2
      

  4.   

    create table tb(id int,name varchar(10),leave int,flag int)
    insert into tb select 1,'电容器1',2,5
    insert into tb select 2,'电容器2',2,5
    insert into tb select 3,'电容器3',2,5
    insert into tb select 4,'电容器4',2,6
    insert into tb select 5,'变压器1',1,0
    insert into tb select 6,'变压器2',1,0
    insert into tb select 7,'电容器5',2,6
    go
    select a.name,b.name as xingzhi
    from tb a inner join tb b on a.flag=b.id and a.name like '电容器%'
    /*
    name       xingzhi
    ---------- ----------
    电容器1       变压器1
    电容器2       变压器1
    电容器3       变压器1
    电容器4       变压器2
    电容器5       变压器2(5 行受影响)*/
    go
    drop table tb
      

  5.   


    if object_id('tb','U') is not null
       drop table tb
    go
    create table tb
    (
     id int identity(1,1),
     name varchar(10),
     leave int,
     flag int
    )
    go
    insert into tb (name,leave,flag)
    select '电容器1',2,5 union all
    select '电容器2',2,5 union all
    select '电容器3',2,5 union all
    select '电容器4',2,5 union all
    select '变压器1',1,0 union all
    select '变压器2',1,0 union all
    select '电容器5',2,6go
    select name,xingzhi=(select top 1 name from tb where id=a.flag) from tb a where flag<>0
    go
    /*
    name       xingzhi
    ---------- ----------
    电容器1       变压器1
    电容器2       变压器1
    电容器3       变压器1
    电容器4       变压器1
    电容器5       变压器2(5 行受影响)*/