A表
店编号 店名
1000  徐汇店
1001  徐汇1支店
1002  徐汇2支店
2000  浦东店
2001  浦东1支店
2002  浦东2支店B表
店编号 收入
1001  50000
1002  80000
2001  60000
2002  90000  结果
店编号 店铺名 支店名    收入
1000  徐汇店 徐汇1支店 50000
1000  徐汇店 徐汇2支店 80000
2000  浦东店 浦东1支店 60000
2000  浦东店 浦东2支店 90000主要是店铺名和支店名都读1张表同一个列

解决方案 »

  1.   

    [code=SQL]declare @ta table(店编号 char(4),店名 varchar(18))
    insert @ta select '1000','徐汇店'
    insert @ta select '1001','徐汇1支店' 
    insert @ta select '1002','徐汇2支店' 
    insert @ta select '2000','浦东店' 
    insert @ta select '2001','浦东1支店' 
    insert @ta select '2002','浦东2支店' declare @tb table(店编号 char(4),收入 int)
    insert @tb select '1001',50000 
    insert @tb select '1002',80000 
    insert @tb select '2001',60000 
    insert @tb select '2002',90000     select * 
    from (select * from @ta where right(店编号,1) = '0') aa
    right join (
    select a.店编号,a.店名,b.收入
    from @ta a
    right join @tb b on a.店编号 = b.店编号) bb
    on left(aa.店编号,3) = left(bb.店编号,3) 
    /*店编号  店名                 店编号  店名                 收入          
    ---- ------------------ ---- ------------------ ----------- 
    1000 徐汇店                1001 徐汇1支店              50000
    1000 徐汇店                1002 徐汇2支店              80000
    2000 浦东店                2001 浦东1支店              60000
    2000 浦东店                2002 浦东2支店              90000(所影响的行数为 4 行)
    */code]
      

  2.   

    select * 
    from @ta aa
    right join (
    select a.店编号,a.店名,b.收入
    from @ta a
    right join @tb b on a.店编号 = b.店编号) bb
    on left(aa.店编号,3) = left(bb.店编号,3) 
    where  right(aa.店编号,1) = '0'
    /*店编号  店名                 店编号  店名                 收入          
    ---- ------------------ ---- ------------------ ----------- 
    1000 徐汇店                1001 徐汇1支店              50000
    1000 徐汇店                1002 徐汇2支店              80000
    2000 浦东店                2001 浦东1支店              60000
    2000 浦东店                2002 浦东2支店              90000(所影响的行数为 4 行)
    */
      

  3.   

    create table t1(
    店编号 varchar(10),
    店名 varchar(20)
    )
    insert t1 select '1000',     '徐汇店' 
    insert t1 select '1001' ,    '徐汇1支店' 
    insert t1 select '1002'  ,   '徐汇2支店' 
    insert t1 select '2000'   ,  '浦东店' 
    insert t1 select '2001'    , '浦东1支店' 
    insert t1 select '2002'     ,'浦东2支店'create table t2(
    店编号 varchar(10),
    收入 int
    )
    insert t2 select '1001',     50000 
    insert t2 select '1002' ,    80000 
    insert t2 select '2001'  ,   60000 
    insert t2 select '2002'   ,  90000select distinct a.店编号,a.店名,b.店名 as 支店名,c.收入
    from
    (
       select 店编号,店名 from t1 where 店名 not like '%支店%'
    ) a,
    (
       select 店编号,店名 from t1 where 店名 like '%支店%'
    ) b,t2 c
    where left(a.店编号,3)=left(b.店编号,3) and b.店编号=c.店编号drop table t1,t2/*
    店编号        店名                   支店名                  收入          
    ---------- -------------------- -------------------- ----------- 
    1000       徐汇店                  徐汇1支店                50000
    1000       徐汇店                  徐汇2支店                80000
    2000       浦东店                  浦东1支店                60000
    2000       浦东店                  浦东2支店                90000(所影响的行数为 4 行)
    */
      

  4.   

    select c.店编号,c.店名 as 店铺名, a.店名 as 支店名,b.收入 from b inner join a on a.店编号=b.店编号 inner join a c on substring(a.店编号,1,1)=substring(c.店编号,1,1) and substring(c.店编号,2,3)='000'
      

  5.   

    declare   @a   table(did char(4),dname varchar(18))
    insert   @a   select   '1000','徐汇店'
    insert   @a   select   '1001','徐汇1支店'  
    insert   @a   select   '1002','徐汇2支店'  
    insert   @a   select   '2000','浦东店'  
    insert   @a   select   '2001','浦东1支店'  
    insert   @a   select   '2002','浦东2支店'  declare   @b   table(did char(4),dsr int)
    insert   @b   select   '1001',50000  
    insert   @b   select   '1002',80000  
    insert   @b   select   '2001',60000  
    insert   @b   select   '2002',90000  select tem1.adid,tem1.adname,tem2.dname,tem1.dsr from (
    select a.did as adid,a.dname as adname,b.did as bdid,b.dsr as dsr 
    from (select did,dname from @a where right(did,1)='0')a 
    left join @b b on left(a.did,3)=left(b.did,3) )tem1
    left join @a tem2  on tem1.bdid=tem2.did1000 徐汇店 徐汇1支店 50000
    1000 徐汇店 徐汇2支店 80000
    2000 浦东店 浦东1支店 60000
    2000 浦东店 浦东2支店 90000我这有点烂 呵呵
      

  6.   


    declare   @ta   table(店编号   char(4),店名   varchar(18)) 
    insert   @ta   select   '1000','徐汇店' 
    insert   @ta   select   '1001','徐汇1支店'   
    insert   @ta   select   '1002','徐汇2支店'   
    insert   @ta   select   '2000','浦东店'   
    insert   @ta   select   '2001','浦东1支店'   
    insert   @ta   select   '2002','浦东2支店'   declare   @tb   table(店编号   char(4),收入   int) 
    insert   @tb   select   '1001',50000   
    insert   @tb   select   '1002',80000   
    insert   @tb   select   '2001',60000   
    insert   @tb   select   '2002',90000          select c.店编号,c.店名,d.支店名,d.收入 from @ta c,
    (select a.店编号,a.店名 支店名,b.收入 from @ta a,@tb b where a.店编号=b.店编号)d
    where left(c.店编号,3)=left(d.店编号,3) and right(c.店编号,1)='0'
      

  7.   

    declare   @ta   table(店编号   char(4),店名   varchar(18)) 
    insert   @ta   select   '1000','徐汇店' 
    insert   @ta   select   '1001','徐汇1支店'   
    insert   @ta   select   '1002','徐汇2支店'   
    insert   @ta   select   '2000','浦东店'   
    insert   @ta   select   '2001','浦东1支店'   
    insert   @ta   select   '2002','浦东2支店'   declare   @tb   table(店编号   char(4),收入   int) 
    insert   @tb   select   '1001',50000   
    insert   @tb   select   '1002',80000   
    insert   @tb   select   '2001',60000   
    insert   @tb   select   '2002',90000           select a.店名 , b.店名 , c.收入 from @ta a,@ta b , @tb c where right(a.店编号,3) = '000' and right(b.店编号,3) <> '000' and left(a.店编号,1) = left(b.店编号,1) and b.店编号 = c.店编号/*
    店名                 店名                 收入          
    ------------------ ------------------ ----------- 
    徐汇店                徐汇1支店              50000
    徐汇店                徐汇2支店              80000
    浦东店                浦东1支店              60000
    浦东店                浦东2支店              90000(所影响的行数为 4 行)
    */
      

  8.   


    if exists (select * from sysobjects where name='A')
    drop table A
    gocreate table A
    (
    [店编号] int primary key,
    [店名] varchar(200)
    )if exists (select * from sysobjects where name='B')
    drop table B
    gocreate table B
    (
    [店编号] int ,
    [收入] int
    )insert into A values ('1000','徐汇店')
    insert into A values ('1001','徐汇1支店')
    insert into A values ('1002','徐汇2支店')
    insert into A values ('2000','浦东店')
    insert into A values ('2001','浦东1支店')
    insert into A values ('2002','浦东2支店')insert into B values ('1001',20000)
    insert into B values ('1002',30000)
    insert into B values ('2001',24000)
    insert into B values ('2002',21000)
    select a.[店编号],a.[店名],c.[店名],b.[收入] from B left join a on a.[店编号]=b.[店编号]
    and substring(convert(varchar,a.[店编号]),0,2)=substring(convert(varchar,b.[店编号]),0,2)
    left join a c on b.[店编号]=c.[店编号]
      

  9.   

    sorry,才发现太匆忙,写错了,不好意思:select a.[店编号],a.[店名],c.[店名],b.[收入] from B left join a on substring(convert(varchar,a.[店编号]),2,3)='000' and substring(convert(varchar,a.[店编号]),0,2)=substring(convert(varchar,b.[店编号]),0,2) left join a c on b.[店编号]=c.[店编号]/*
    店编号         店名                   店名                   收入          
    ----------- -------------------- -------------------- ----------- 
    1000        徐汇店                  徐汇1支店                20000
    1000        徐汇店                  徐汇2支店                30000
    2000        浦东店                  浦东1支店                24000
    2000        浦东店                  浦东2支店                21000
    */