表A字段
creat table bomlist(
[id] [varchar](20),
[parentid] [varchar](20),
[qty] [decimal] (9,4)
PRIMARY KEY (Id,parentid))
表B
creat table itemlist(
[Id] [varchar](20),
[itemname] [varchar](20),
[unit] [varchar](5)
PRIMARY KEY (Id,unit))insert into bomlist(
select '1','01',0
union all select '2','02',1
union all select '3','03',2
union all select '4','04',3
union all select '5','05',4
union all select '6','06',5insert into itemlist(
select '1','1明细','单位1'
union all select '2','2明细','单位2'
union all select '3','3明细','单位3'
union all select '4','4明细','单位4'
union all select '5','5明细','单位5'
union all select '6','6明细','单位6'
union all select '01','01明细','单01'
union all select '02','02明细','单02'
union all select '03','03明细','单03'
union all select '04','04明细','单04'
union all select '05','05明细','单05'
union all select '06','06明细','单06'
union all select '07','07明细','单07'
union all select '08','08明细','单08'
union all select '09','09明细','单09'要求一个最简单的查询
结果是
1 1明细 单位1 01 01明细 单01 0
2 2明细 单位2 01 02明细 单02 1
3 3明细 单位3 01 03明细 单03 2
4 4明细 单位4 01 04明细 单04 3
5 5明细 单位5 01 05明细 单05 4
6 6明细 单位6 01 06明细 单06 5

解决方案 »

  1.   

    select a.id,b.itemname,b.unit,a.parentid,c.itemname,c.unit,a.qty
    from bomlist a
    left join itemlist b on a.id=b.id
    left join itemlist c on a.parentid=c.id/**
    id                   itemname             unit  parentid             itemname             unit  qty
    -------------------- -------------------- ----- -------------------- -------------------- ----- ---------------------------------------
    1                    1明细                  单位1   01                   01明细                 单01   0.0000
    2                    2明细                  单位2   02                   02明细                 单02   1.0000
    3                    3明细                  单位3   03                   03明细                 单03   2.0000
    4                    4明细                  单位4   04                   04明细                 单04   3.0000
    5                    5明细                  单位5   05                   05明细                 单05   4.0000
    6                    6明细                  单位6   06                   06明细                 单06   5.0000(6 行受影响)
    **/
      

  2.   


    select *
    from bomlist a
    outer apply(select itemname,unit from itemlist where a.id=id) b
    outer apply(select itemname,unit from itemlist where a.parentid=id) c
    /*
    id parentid qty itemname unit itemname unit
    1 01 0.0000 1明细 单位1 01明细 单01
    2 02 1.0000 2明细 单位2 02明细 单02
    3 03 2.0000 3明细 单位3 03明细 单03
    4 04 3.0000 4明细 单位4 04明细 单04
    5 05 4.0000 5明细 单位5 05明细 单05
    6 06 5.0000 6明细 单位6 06明细 单06
    */
      

  3.   

    select a.id,b.itemname,b.unit,parentid,c.itemname,c.unit,qty
    from bomlist a,itemlist b,(select * from itemlist where len(Id)=2)c
    where a.id=b.Id and parentid=c.Idselect a.id,b.itemname,b.unit,parentid,c.itemname,c.unit,qty
    from bomlist a,itemlist b,(select * from itemlist where Id in (select parentid from bomlist))c
    where a.id=b.Id and parentid=c.Id/*结果
    id  itemname  unit   parentid  itemname  unit  qty
    -----------------------------------------------------------
    1 1明细 单位1 01 01明细 单01 0.0000
    2 2明细 单位2 02 02明细 单02 1.0000
    3 3明细 单位3 03 03明细 单03 2.0000
    4 4明细 单位4 04 04明细 单04 3.0000
    5 5明细 单位5 05 05明细 单05 4.0000
    6 6明细 单位6 06 06明细 单06 5.0000
    */