我想合并TableA和TableB中的值,并优先考虑TableA中的bundled_shipping_cost。仅当TableA的ID在TableA中不存在时,才应使用TableB的shipping_cost值。
是否有推荐的联接语句?
表A
1 | 100 |
2 | 200 |
3 | 300 |
4 | 400 |
TableB
1 | 95 |
2 | 195 |
5 | 50 |
6 | 75 |
TableC
1 | 100 |
2 | 200 |
3 | 300 |
4 | 400 |
5 | 50 |
6 | 75 |
答案 0 :(得分:1)
Select
查询。TableA
获取所有行。 Select
查询中,仅从TableB
获取那些不包含的行
使用TableA
和Left Join
在Where TableA.id IS NULL
中存在。Union ALL
他们:尝试:
SELECT dt.id,
dt.final_shipping_cost
FROM (
SELECT t1.id,
t1.bundled_shipping_cost AS final_shipping_cost
FROM TableA AS t1
UNION ALL
SELECT t2.id,
t2.shipping_cost AS final_shipping_cost
FROM TableB AS t2
LEFT JOIN TableA AS t1 ON t1.id = t2.id
WHERE t1.id IS NULL
) AS dt
ORDER BY dt.id
答案 1 :(得分:0)
尝试此查询:
select q.*from(SELECT *
FROM `t1`
union select * from t2) as q group by id