Table1: temp1
temp1Id Size
1 10
2 20
3 30
Table2: temp2
temp2Id Size Name
1 10 AAA
2 20 BBB
3 20 CCC
select * from dbo.temp1
left join dbo.temp2 on temp1.Size=temp2.Size
temp1Id Size temp2Id Size Name
1 10 1 10 AAA
2 20 2 20 BBB
2 20 3 20 CCC
3 30 NULL NULL NULL
select * from dbo.temp1
left join dbo.temp2 on temp1.Size=temp2.Size
where temp2.Name=‘AAA‘
temp1Id Size temp2Id Size Name
1 10 1 10 AAA
select * from dbo.temp1
left join dbo.temp2 on temp1.Size=temp2.Size
and temp2.Name=‘AAA‘
temp1Id Size temp2Id Size Name
1 10 1 10 AAA
2 20 NULL NULL NULL
3 30 NULL NULL NULL
总结:
on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。
时间: 2024-10-25 08:58:19