How to find all the foreign key records that do not have a primary key using a subquery (a query inside a query). Subquerys can produce the same result set as a Join as well the opposite of a join result set 如何利用子查詢找出沒有主關鍵值的資料。子查詢可產生與join相反的結果。

src: [Video 42 - SQL SubQuery](https://thefrugalcomputerguy.com/grouppg.php?ser=15&grp=15)(https://youtu.be/X87P7ADNF_4)

 

join 以外的資料

select distinct
  "phn"."employee-id"
from
 "tbl_emp-phn-original" as "phn"
where "phn"."employee-id" not in
	(
	select
		"emp".employee-id"
	from
		"tbl_employees-original" as "emp"
	)
 

第2支

delete from
  "tbl_emp-phn-orignial"
where
  "employee-id" in (
			select distinct
				"emp-phn"."employee-id"
			from
				"tbl_emp-phn-original" as "emp-phn"
			where
				"emp-phn"."employee-id" not in
					(select
						"emp"."employee-id"
					from
						"tbl_employees" as "emp"
					)
		    )

arrow
arrow
    文章標籤
    libreoffice base
    全站熱搜
    創作者介紹
    創作者 理群 的頭像
    理群

    呂理群的部落格

    理群 發表在 痞客邦 留言(0) 人氣()