SQL多表聯合查詢(LEFT JOIN)條件差異
阿新 • • 發佈:2017-06-02
logs 技術分享 .cn where 聯合查詢 uid exist 包含 into
查詢A:
select a.*,b.* into Bus605115_ON_Where_And --(642 行受影響)
from PositionN a left join szt b
on b.[fQCBH]=a.fBusNo
where fdealtime between fDInTime and fDInTimeN
and fbusno=605115
查詢B:
select a.*,b.* into Bus605115_ON_And_Where --(835 行受影響)
from PositionN a left join szt b
on b.[fQCBH]=a.fBusNo
and fdealtime between fDInTime and fDInTimeN
where fbusno=605115
查詢A、B的結果差異:
select * from Bus605115_ON_And_Where a
where not exists
(
select 1 from Bus605115_ON_Where_And b
where a.fGuid=b.fGuid
)
order by a.fDInTime
查詢A中只有兩表互相之間能關聯的結果;
查詢B中還包含未與b關聯的結果,因此記錄數比查詢A多;
SQL多表聯合查詢(LEFT JOIN)條件差異