---
title: "SQL语句中的两个常用的关键字:INTERSECT(交集)和UNION(并集)"
author: "cms163"
date: "2010-02-04T15:18:24Z"
tags: []
categories: ["数据库"]
slug: "sql-intersect-union"
draft: false
lastmod: "2010-02-04T15:18:24Z"
id: 587540
url: "https://cms163.iteye.com/blog/587540"
---

查询几张表中并集
select b.* from CorpBasic b where  1=1 
and b.ID in(
	select CorpID from CorpSMS  where 1=1  and RoleID in(-999 ,1) 
	**union**
	select CorpID from CorpWap where 1=1  and RoleID in(-999 ,1)
	**union**
	select CorpID from CorpComWeb where 1=1  and RoleID in(-999 ,1)
      )

查询几张表中交集
select b.* from CorpBasic b where  1=1 
and b.ID in(
	select CorpID from CorpSMS  where 1=1  and RoleID in(-999 ,1) 
	**intersect**
	select CorpID from CorpWap where 1=1  and RoleID in(-999 ,1)
	**intersect**
	select CorpID from CorpComWeb where 1=1  and RoleID in(-999 ,1)
      )