我需要在一个大表中加入表A和表B.在每张桌子上我都有(身份,品牌,力量)。
id | brand | power
-------------------
1 | BMW | 500
2 | SKODA | 220
3 |PORSCHE| 450
id | brand | power
-------------------
1 | BMW| 500
2 | SKODA| 220
4 |FERRARI| 600
5 | HUMMER| 350
结果应为:
| A.brand | A.power | B.brand | B.power
------------------------------------------
1 | BMW | 500 | BMW | 500
2 | SKODA | 220 | SKODA | 220
3 | PORSCHE | 450 | null | null
4 | null | null | FERRARI | 600
5 | null | null | HUMMER | 350
这是我尝试过的,但似乎没有效果。
SELECT
A.id,A.brand,A.[power],B.brand,B.[power]
FROM A
FULL JOIN B on A.id = B.id
任何帮助?
答案 0 :(得分:0)
你应该使用完整的句子:
SELECT
A.id,A.brand,A.power,B.brand,B.power
FROM tableA A
FULL OUTER JOIN tableB B on A.id = B.id
答案 1 :(得分:0)
我知道这是一种蛮力,但它暂时有效:
SELECT a.id,
a.brand,
a.[power],
b.brand,
b.[power]
FROM a
LEFT OUTER JOIN b
ON a.id = b.id
UNION
SELECT id,
NULL AS brand,
NULL AS power,
brand,
power
FROM b
WHERE id NOT IN (SELECT id
FROM a)