日期:2014-06-10  浏览次数:20550 次

多表查询

/*基本连接*/
select a.Name,b.Name
from T_Employee a,T_Department b
where a.DepartmentId=b.Id

/*内连接,消除没有匹配连接条件的项*/
select a.Name,b.Name
from T_Employee a inner join T_Department b
on a.DepartmentId=b.Id

select a.Name,b.Name
from T_Employee a inner join T_Department b
on a.DepartmentId=b.Id and b.IsDelete=1

select a.Name,b.Name
from T_Employee a inner join T_Department b
on a.DepartmentId=b.Id 
where  b.IsDelete=1


select a.Name,b.Name
from T_Employee a  join T_Department b--inner可以省略
on a.DepartmentId=b.Id 
where  b.IsDelete=1


/*左外连接,保留没有匹配连接条件的左边表的项*/
select a.Name,b.Name
from T_Employee a left outer join T_Department b--outer可以省略
on a.DepartmentId=b.Id

select a.Name,b.Name
from T_Employee a left outer join T_Department b--outer可以省略
on a.DepartmentId=b.Id and b.IsDelete=1--满足此连接条件的才显示b表内容,保留左边表的没有匹配连接条件的项


select a.Name,b.Name
from T_Employee a left outer join T_Department b--outer可以省略
on a.DepartmentId=b.Id
where  b.IsDelete=1--相当于先执行前三行,再用where语句进行筛选


/*右外连接,保留没有匹配连接条件的右边表的项*/
select a.Name,b.Name
from T_Employee a right outer join T_Department b--outer可以省略
on a.DepartmentId=b.Id 

select a.Name,b.Name
from T_Employee a right outer join T_Department b--outer可以省略
on a.DepartmentId=b.Id and b.IsDelete=1--满足此连接条件的才显示a表内容,保留右边表的没有匹配连接条件的项


select a.Name,b.Name
from T_Employee a right outer join T_Department b--outer可以省略
on a.DepartmentId=b.Id
where  b.IsDelete=1--相当于先执行前三行,再用where语句进行筛选


/*全连接,先列出左边表的所有项,保留没有匹配连接条件的左右两边表的项*/
select a.Name,b.Name
from T_Employee a full outer join T_Department b--outer可以省略
on a.DepartmentId=b.Id 

select a.Name,b.Name
from T_Employee a full outer join T_Department b--outer可以省略
on a.DepartmentId=b.Id and b.IsDelete=1--先列出左边表的所有项,保留没有匹配连接条件的左右两边表的项


select a.Name,b.Name
from T_Employee a full outer join T_Department b--outer可以省略
on a.DepartmentId=b.Id
where  b.IsDelete=1--相当于先执行前三行,再用where语句进行筛选


/*交叉连接,select的b表的字段分别来匹配所有的select的a表字段*/
select a.Name,b.Name
from T_Employee a cross  join T_Department b

select a.Name,b.Name
from T_Employee a cross  join T_Department b
where  b.IsDelete=1--相当于先执行前两行,再用where语句进行筛选


/*自连接,在同一个表上进行连接*/
select a.Name,a.IsDelete, b.Name
from T_Department a,T_Department b
where a.IsDelete=b.IsDelete

select a.Name,a.IsDelete, b.Name
from T_Department a,T_Department b
where a.IsDelete=b.IsDelete and a.Id<>b.Id and a.Name='广东省'

 

/*联合查询,将两个查询的结果连接起来,所以select的个数必须相同,类型必须可转换为相同的,可以空起来占位
,union all表示显示所有的列,尽管出现重复,union 表示只显示不重复的信息
*/
select a.Name ,a.BaseSalary,b.Name
from T_Employee a,T_Department b
where a.DepartmentId=b.Id and a.GenderId='403A0BD5-A304-4981-9535-ADA7AF2BFB51'
union
select '',SUM(a.BaseSalary),''
from T_Employee a,T_Department b
where a.DepartmentId=b.Id  and a.GenderId='403A0BD5-A304-4981-9535-ADA7AF2BFB51'
union
select '',AVG(a.BaseSalary),''
from T_Employee a,T_Department b
where a.DepartmentId=b.Id  and a.GenderId='403A0BD5-A304-4981-9535-ADA7AF2BFB51'
order by BaseSalary


/*子查询*/
select a.Name ,a.BaseSalary,b.Name
from T_Employee a,T_Department b
where a.DepartmentId=b.Id 
and a.GenderId='403A0BD5-A304-4981-9535-ADA7AF2BFB51'
and a.BaseSalary< (select AVG(a.BaseSalary)+1
from T_Employee a,T_Department b
where a.DepartmentId=b.Id 
and a.GenderId='403A0BD5-A304-4981-9535-ADA7AF2BFB51'
)


/*子查询嵌套*/
select a.Name ,a.BaseSalary,b.Name
from T_Employee a,T_Department b
where a.DepartmentId=b.Id 
and a.GenderId='403A0BD5-A304-4981-9535-ADA7AF2BFB51'
and a.name in (select Name
from T_Employee a,T_SalarySheet b
where a.DepartmentId=b.DepartmentId
and b.Year=(select Year from T_SalarySheet where Id='E0AD51DE-C6A2-4BBE-BF85-D978EAEF9BE9'
)
)


/*exists 如果exists有结果,就开始查询主select*/
select a.Name ,a.BaseSalary,b.Name
from T_Employee a,T_Department b
where exists(select * from T_Employee where Name='')


declare @address varchar(20)
declare @pwd varchar(10)
set @address='他告诉'
set @pwd=''
if exists(select * from T_Employee a where a.Address =@address and a.name =@pwd )
print '登录成功'
else
print '登录失败'


/*交查询,得到相交的部分*/
select a.Name ,a.BaseSalary
from T_Employee a
intersect
select a.Name ,a.BaseSalary from T_Employee a where Name=''



/*差查询,在第一个select中去除这两个相交的部分*/
select a.Name ,a.BaseSalary
from T_Employee a
except
select a.Name ,a.BaseSalary from T_Employee a where Name=''