我有表_1 和表_2 表
_1
id name cost
100 joe 10
101 bob 20
102 mary 30
表_2
id name
100 joe
101 bob
102 mary
103 tom
我想加入這些表,即使沒有匹配 id 并期望結果
id name cost
100 joe 10
101 bob 20
102 mary 30
103 tom null
我的查詢
select t1.id, t1.name, t1.column1, t1.column2
from Table_1 t1
join Table_1 t2 on t1.id = t2.id
and t1.id <> t2.id
我什么也沒有得到回報。需要一些幫助。謝謝
uj5u.com熱心網友回復:
看起來你需要一個簡單的外連接
select t2.id, t2.name, t1.cost
from table_2 t2
left join table_1 t1 on t1.id=t2.id
uj5u.com熱心網友回復:
你需要的是一個right join(或者left join如果你顛倒連接中表的順序)
select table_2.id, table_2.name, table_1.cost
from table_1 right join table_2
on table_1.id = table_2.id
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/422051.html
標籤:
上一篇:SQLServer-優化查詢
下一篇:如何在SQL中轉換日期格式?
