如何避免查询表中的重复值

时间:2017-07-07 07:47:30

标签: sql

3表在这里

T1:

Id   a
1   100
1   600
2   800

T2:

Id    b
1    600
2    700
3    400

T3:

Id    c
2    400
3    800
4    100

下面的sql代码运行....

SELECT  t.id, t1.a, t2.b, t3.c
FROM (((select id 
        from t1
        union 
        select id
        from t2 
        union 
        select id 
        from t3 )  AS t
        LEFT JOIN t1 ON t.id = t1.id)
    LEFT JOIN t2 ON t.Id = t2.Id)
 LEFT JOIN t3 ON t.Id = t3.Id;

获得结果并重新编码重复值,如此

id   a      b     c
1   600     600  400
1   100     600  400
2   800     700 
3           400  800
4           100 

但我希望结果避免重复值如下:

Id     a        b      c 
 1    100      600    400
 1    600       -      -
 2    800      700     -         
 3     -       400    800
 4     -        -     100

ples help .........

1 个答案:

答案 0 :(得分:0)

这个怎么样?

WHERE t1.a <> t2.b
AND t1.a <> t3.c
AND t2.b <> t3.c