例如,我有两个表:tbMetals和tbSells
tbMetals
mId - name - cost
1 - Iron - 100
2 - Steel - 200
3 - Copper - 300
4 - Zinc - 150
tbSells
CustomerId - mId - SellDate - weight
100 - 1 - 2017/1/1 - 1.2
100 - 2 - 2017/1/2 - 3.5
111 - 1 - 2017/3/3 - 2.1
我需要ClientId = 100的这个输出
mId - SellDate - weight
1 - 2017/1/1 - 1.2
2 - 2017/1/2 - 3.5
3 - NULL - NULL
4 - NULL - NULL
答案 0 :(得分:1)
这是一个简单的左连接。
select
m.mId
,s.SellDate
,s.weight
from
tbMetals m
left join
tbSells s on
s.mId = m.mId
and s.ClientId = 100 --optional, if you really want it.