table1:id   unit
1     克
2    千克
3     吨table2:
id1  id2  coe
 1    2   1000
 2    3   1000
table2中的id1和id2来自table1中的id现在求一句最精简的语句,显示出如下信息: id1 id2  coe
 克 千克  1000
千克 吨   1000

解决方案 »

  1.   

    select a1.unit id1,a2.unit id2,b.coe
    from table2 b
    join table1 a1 on a1.id=b.id1
    join table1 a2 on a2.id=b.id2
      

  2.   

    create table #tb1
    (
      id int,
      unit nvarchar(10)
    )
    insert into #tb1 select 1,'克'
    insert into #tb1 select 2,'千克'
    insert into #tb1 select 3,'吨'
    create table #tb2
    (
      id1 int,
      id2 int,
      coe int
    )
    insert into #tb2 select 1,2,1000
    insert into #tb2 select 2,3,1000select a1.unit as id1,a2.unit as id2,B2.coe
    from #tb2 B2 join
    #tb1 a1
    on a1.id=B2.id1
    join 
    #tb1 a2
    on a2.id=B2.id2
    id1        id2        coe
    ---------- ---------- -----------
    克          千克         1000
    千克         吨          1000(2 行受影响)
      

  3.   


    select a1.unit id1,a2.unit id2,b.coe
    from table2 b,table1 a1,table1 a2 
      where a1.id=b.id1
    and  a2.id=b.id2
      

  4.   

    两张表的内连接查询了
    inner outer join