sql查詢兩個表的交集 如何使用SQL語句求出交集?
如何使用SQL語句求出交集?intersection的關(guān)鍵字是intersect,例如:select*from EMP where deptno in(10,20)intersect select*f
如何使用SQL語句求出交集?
intersection的關(guān)鍵字是intersect,例如:select*from EMP where deptno in(10,20)intersect select*from EMP where deptno in(20,30)
求多個表交集的SQL語句是什么呀?
使用exists和notexists查找交集和差集。exists和notexists引入的子查詢可用于操作兩個集合:交集和差集。兩個集合的交集包含屬于兩個原始集合的所有元素。差集包含只屬于兩個集合中第一個集合的元素。城市專欄中作者和出版商的交集是作者和出版商共同生活的城市的集合。使用PubsSelectDistinctCityFromAuthorsWhereExists(從PU中選擇*blishersWHEREauthors.city=出版商. city)下面是結(jié)果集:city-----Berkeley(1row(s)impacted)當然,查詢可以寫成簡單的連接。使用Pubss作者的選民區(qū).cityforAuthorsInnerJoinPublishersOnAuthors.城市=出版商.citycity專欄中作者和出版商的差異集是作者居住但沒有出版商居住的所有城市的集合,即除了伯克利以外的所有城市。使用PubsSelectDistinctCityFromAuthorsWherenotexists(選擇*FROMpublishersWHEREauthors.city=出版商. 城市)查詢也可以寫成:use pubs select distinct city from authors where economy notin(select city from publisher)參考:sqlserver Books Online