表 a     字段  id  sFrom    sTo   value
                1    xx     xxx    100
                2    pp     ppp    200
               
表b      字段  id   name   code
                1   北京    xx
                2   天津    xxx
                3   上海    pp
                4   重庆    ppp根据a.id 结果集     1   xx  北京  xxx  天津   100这条sql语句怎么写?

解决方案 »

  1.   


    select * from
    a left join b on a.sFrom=b.code
      

  2.   

    错了,你这种情况需要嵌套join
    你还不如弄一个视图呢
      

  3.   


    select a.sFrom,b.name,a.sTo,b.code,a.value from a inner join b on a.id=b.id and a.id='???'
      

  4.   

    select a.id,b.name,a.sto,a.value from a left join b on a.sform=b.code
      

  5.   

      
    a.id=b.id  ????
      

  6.   

    select aa.id,cc.name,dd.name,aa.value 
    from a  aa,b  cc,b  dd
    where a.sto=* cc.id
    and a.sFrom =* dd.id
      

  7.   

    select aa.id,cc.name,dd.name,aa.value  
    from a aa,b cc,b dd
    where aa.sto=* cc.id
    and aa.sFrom =* dd.id
      

  8.   

    就是分别在 from  和 to 字段的后面显示对应的城市名称
      

  9.   

    select a.id,a.sfrom,b.name,a.sto,a.value from a left join b on a.sfrom=b.code and a.sto=b.code
    go
      

  10.   


    select a.id,a.sFrom,b.name,a.sTo,(slelect b.name from b where code a.sTo) from a,b where a.sFrom=b.code
      

  11.   


    create table a (id int,sFrom varchar(20),sTo varchar(20),value int)
    insert  a 
    select 1,'xx','xxx',100 union all
    select 2,'pp','ppp',200 
    ----------------------------
    create table b (id int,name varchar(20),code varchar(20))
    insert b
    select 1,'北京','xx' union all
    select 2,'天津','xxx' union all
    select 3,'上海','pp' union all
    select 4,'重庆','ppp'----
    select a.id,a.sFrom,b.name,a.sTo,(select b.name from b where code= a.sTo) from a,b where a.sFrom=b.code
    /*
    1 xx 北京 xxx 天津
    2 pp 上海 ppp 重庆
    */
      

  12.   


     select a.id,
            a.sForm,
            s.name,
            a.sTo,
            c.name,
            a.value
       from a,b,b c
      where a.id = '1'
        and a.sForm = b.code
        and a.sTo = c.code
      

  13.   

    稍微改下: select distinct
            a.id,
            a.sForm,
            b.name,
            a.sTo,
            c.name,
            a.value
       from a,b,b c
      where a.id = '1'
        and a.sForm = b.code
        and a.sTo = c.code
      

  14.   

     select bb.id,bb.sfrom,bb.name,bb.sto,b.name,bb.value from (select distinct aa.* from(select a.id,a.sfrom,b.name,a.sto,a.value from a left join b on a.sfrom=b.code) as aa) as bb left join b on bb.sto=b.code
    go
      

  15.   

    select a.sFrom,b.name,a.sTo,(select b.name from b where b.code= a.sTo),a.value from  a left join b on a.sfrom=b.code
    go
      

  16.   

    表 a 字段 id sFrom sTo value
      1 xx xxx 100
      2 pp ppp 200
       
    表b 字段 id name code
      1 北京 xx
      2 天津 xxx
      3 上海 pp
      4 重庆 ppp根据a.id 结果集  1 xx 北京 xxx 天津 100
    select a.id, a.sFrom,b1.name,a.sTo,b2.name , a.value
    from a 
    left join b b1 on a.sForm=b1.ccode
    left join b b2 on a.sTo=b2.ccode