我有两张桌子 -
T1:
col_1 col_2 col_3
10001 apple 3
10001 orange 2
10001 grapes 5
T2:
col_1 col_2 col_3 col_4
10001 apple 3 123
10001 orange 2 345
哪个SQL查询会加入这两个表;给我这样的结果:
col_1 col_2 col_3 col_4
10001 apple 3 123
10001 orange 2 345
10001 grapes 5
答案 0 :(得分:1)
连接表,然后使用COALESCE获取第一个非空数据列
SELECT COALESCE(t1.col_1,t2.col_1) col_1,
COALESCE(t1.col_2,t2.col_2) col_2,
COALESCE(t1.col_3,t2.col_3) col_3,
COALESCE(t2.col_4,'') col_4,
FROM t1
FULL JOIN t2 ON t1 ON t1.col_1 = t2.col1 AND t1.col_2 = t2.col_2 AND t1.col_3 = t2.col_3
答案 1 :(得分:0)
(SELECT col_1, col_2, col_3
FROM t1)
UNION DISTINCT
(SELECT col_1, col_2, col_3
FROM t2)
答案 2 :(得分:0)
SELECT t1.col_1, t1.col_2, t1.col3, t2.col_4
FROM t1
LEFT OUTER JOIN t2 ON t1.col_1 = t2.col_1 AND t1.col_2 = t2.col_2 AND t1.col_3 = t2.col_3
答案 3 :(得分:0)
SELECT t1.*,
col_4
FROM t1
LEFT JOIN t2 using(col_2);
如果col_1和col_2标识唯一条目,则将最后一行更改为:
LEFT JOIN t2 using(col_1,col_2);
如果col_1,col_2,col_3标识唯一条目,则将最后一行更改为:
LEFT JOIN t2 using(col_1,col_2,col_3);