表table1 有A,B,C,D4个字段,且a,b组合不唯一,d唯一
表table2 有A,E,F三个字段,A唯一
表table3 有B,G,H三个字段,B唯一
要求取a,b,c,d,e,f,g,h所有值,条件是ab组合唯一
这个sQL语句怎么写?

解决方案 »

  1.   

    select A,B,C,D,E,F,G,H from table2 a cross join table3 b right join table1 c on c.a=a.a and c.b=b.b
      

  2.   

    这样会不会漏掉table1的一些数据啊?
      

  3.   

    不可能吧
    比如:
    A =1 B = 2 D =3
    A =1 B = 2 D =4
    这两条记录选哪条呢?
      

  4.   

    select * from
    (select * from t1 t where d = (select top 1 d from t1 where a=t.a and b = t.b)) a
    left join t2 b on a.a = b.a
    left join t3 c on a.b = c.b
      

  5.   

    select * from
    (select * from t1 t where d = (select top 1 d from t1 where a=t.a and b = t.b)) a
    full join t2 b on a.a = b.a
    full join t3 c on a.b = c.b
      

  6.   

    想到一条思路,就是t1表的A,B字段合成一个字段,这样是不是做起来简单点?