作者adrianshum (Alien)
看板Database
標題Re: [SQL ] 查詢table1,2某欄位相異的資料
時間Sat Dec 31 00:26:14 2011
※ 引述《iwhiori (玉米)》之銘言:
: select a.id from a where a.id not in (select b.id from b)
: From http://stackoverflow.com
還可以試試
select a.id from a
minus
select b.id from b
或 a left join b,找出 b 是 null 的:
select a.id from a
left join b on a.id = b.id
where b.id is null
not in 一般會建議寫 not exists, 大部份情況下速度會較佳
select a.id from a where
not exists (select 1 from b where b.id = a.id)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 183.179.61.91
※ 編輯: adrianshum 來自: 183.179.61.91 (12/31 00:27)
推 TeemingVoid:推 Alien大大果然厲害! ^^ Exists那段很精采 12/31 01:17
推 iwhiori:推!!精彩! 12/31 10:22