SQL5多表查询.docx

  1. 1、本文档共5页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
SQL5多表查询

5多表查询create database sdpb1create table dept(deptno int primary key,dname nvarchar(30),loc nvarchar(30))create table emp(empno int primary key,ename nvarchar(30),job nvarchar(30),mgr int,hiredate datetime,sal numeric(10,2),comm numeric(10,2),--创建外键deptno int foreign key references dept(deptno))--两张表关连--显示所有部门的员工信息select * from emp,dept where emp.deptno = dept.deptno ---如何显示销售部门员工的信息 select * from emp,dept where dept.dname = sales and emp.deptno = dept.deptno--显示销售部门员工的名字部门工资和工作地--1如果多张标有相同名字的字段 需要带上表名(或表的别名)select ename,emp.deptno,loc,sal from emp,dept where dept.dname = sales and emp.deptno = dept.deptno --2别名 select ename,雇员.deptno,loc,sal from emp as 雇员,dept as 部门 where 部门.dname = sales and 雇员.deptno = 部门.deptno --如何显示部门为10 的员工名字和工资并按工资排名 select ename,sal from emp ,dept where dept.deptno = 10 and dept.deptno = emp.deptno order by sal--显示雇员名,工资及所在部门名按部门名排序部门内部用sal排序 select ename,sal,dept.deptno from emp ,dept where dept.deptno = emp.deptno order by dept.deptno,sal--2自连接(同一张表)--显示某个员工的上级领导的的姓名 --1 显示每个员工的姓名和他的上级 select ename from emp where empno =(select mgr from emp where ename = miller) --分析将表看成两张表 员工emp与上级emp select 员工.ename 员工名,上级.ename as 上级名 from emp as 员工 ,emp as 上级 where 员工.mgr = 上级.empno--3子查询 --1)单行子查询 :返回结果为单行 用 = 或 in均可 --显示与smith同一个部门的员工select * from emp where deptno =--用 = 或 in均可(select deptno from emp where ename = smith)--单行是指这里 --2)多行子查询 :返回结果为多行 用 in --显示10号部门的工作岗位相符的员工信息 --如何排除10号部门本身 select * from emp where job in--只可以用in (select distinct job from emp where deptno = 10) and ( deptno != 10) --3)在from子句中使用子查询 --显示高于部门平均工资的人 --1 找出每个部门的平均工资 select AVG(sal),deptno from emp group by deptno --2 把上面的查询结果当作一个临时表 select ename,myavg,sal from emp , (select AVG(sal) as myavg,deptno from emp group by deptno) as temp--临时表 where emp.deptno = temp.deptno and emp.saltemp.myavg order by sal desc外连接(左外连接 与 右外连接) --左外连 : 如果左边的表的记录全部要显示 如果没有匹配的记录就用空来填 --右外连 : 如果右边的表的记录全部要显示 如果没有匹配的记录就用空来填 --显示每个员工的姓名

文档评论(0)

xy88118 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档