2.多表查询

  1. 笛卡尔积错误:多表查询必须有连接条件,否则会产生笛卡尔积错误,得到数据条数=每个表数据条数的积
select last_name,employees.department_id, department_name from employees, departments

注意:查询的字段在多个表同时存在必须带表名

  1. 内连接:合并具有同一列的两个以上的表的行, 结果集中不包含一个表与另一个表不匹配的行
    等值连接(两个表)
select last_name,e.department_id, department_name from employees e, departments d where e.department_id = d.department_id

等值连接(多个表)

select last_name,e.department_id, department_name,l.city from employees e, departments d, locations l where e.department_id = d.department_id and d.location_id = l.location_id

连接n个表,需要n-1个连接条件

非等值连接:

select employee_id,last_name,salary,grade_level from employees e,job_grades j where e.salary between j.lowest_sal and j.highest_sal

自连接:
查询公司中员工‘chen’的manager信息:

select emp.last_name,manager.last_name,manager.salary,manager.email from employees emp, employees manager
where emp.manager_id = manager.employee_id and lower(emp.last_name) = 'chen'
  1. 外连接:两个表在连接过程中除了返回满足连接条件的行以外还返回左(或右)表中不满足条件的行 ,这种连接称为左(或右) 外连接
    左外连接:
select last_name,e.department_id, department_name from employees e, departments d where e.department_id = d.department_id(+)

右外连接:

select last_name,e.department_id, department_name from employees e, departments d where e.department_id(+) = d.department_id
  1. SQL:1999语法:
    自然连接(natural join):自动连接两个表中所有相同的字段,查询的字段名不能使用限定词
select last_name,department_id, department_name from employees e natural join departments d

useing:查询字段不能使用限定词,两个表的连接字段名和数据类型必须相同才能使用using

select last_name,department_id, department_name from employees e join departments d using(department_id)

内关联:join on格式

select last_name,e.department_id, department_name from employees e join departments d on e.department_id = d.department_id
select last_name,e.department_id, department_name,l.city from employees e 
join departments d on e.department_id = d.department_id
join locations l on d.location_id = l.location_id

外关联:outer可以省略
左外关联:

select last_name,e.department_id, department_name from employees e left outer join departments d on e.department_id = d.department_id

右外关联:

select last_name,e.department_id, department_name from employees e right outer join departments d on e.department_id = d.department_id

全外关联:

select last_name,e.department_id, department_name from employees e full outer join departments d on e.department_id = d.department_id

练习题:

  1. 显示所有员工的姓名,部门号和部门名称。
select last_name,e.department_id,department_name
from employees e, departments d
where e.department_id = d.department_id(+);
  1. 查询90号部门员工的job_id和90号部门的location_id
select distinct job_id,location_id
from employees e left join departments d
on e.department_id = d.department_id
where d.department_id = 90
  1. 选择所有有奖学金的员工的last_name,department_name,location_id,city
select last_name,department_name,d.location_id,city
from employees e
join departments d on e.department_id = d.department_id
join locations l on d.location_id = l.location_id
where e.commission_pct is not null;
  1. 选择city在Toronto工作的员工的last_name,job_id,department_id,department_name
select last_name,job_id,e.department_id,department_name
from employees e
join departments d on e.department_id = d.department_id
join locations l on d.location_id = l.location_id
where l.city = 'Toronto';
  1. 自连接练习
select e1.last_name "employees",e1.employee_id "Emp#",e2.last_name "Manager",e2.employee_id "Mgr#"
from employees e1,employees e2
where e1.manager_id = e2.employee_id(+);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
多薄多合并——excel 外接程序插件 使 用 说 明 一、功能: 1、多薄合并:将某一文件夹或当前文件夹下所有工作薄合并到一个自动新建的“汇总”工作薄中。每个工作薄中可含多个不同的工作,工作与工作一一对应(合并)。有“保留重复行(默认)”、“去除重复行”两种方式。 默认合并当前文件夹下的所有工作薄;在启动excel后(未保存)的新工作薄中点击该按钮则打开“文件夹选择”对话框。 2、多合并:将当前工作薄中所有工作合并到一个自动生成且位于最后的“汇总”工作中。有“保留重复行(默认)”、“去除重复行”两种方式。 3、清除数据:清除当前工作或当前工作薄中所有工作数据。 默认“取消”按钮(即按Enter后)仅清除当前工作中的数据。 4、分类汇总:将所选区域第一列相同行的各列数据进行汇总(求和),并将汇总结果放置到所选目标区域。汇总结果为第一列不再有重复行,即第一列“去除重复行”,其后各列数据求和或文本联接。 5、拆分格:将所选区域第一列中不同行拆分为许多工作,以第一列各行内容为工作名称,并复制其后面各列内容。 6、选择查询:在所选区域第一列中查找,并将查找结果(符合查询条件的许多行)放置到所选择的目标区域。 7、多薄汇总——求和(默认)、全部求和、平均、计数:将某一文件夹或当前文件夹下的所有工作薄中的当前工作的数据按照所选择的单元格区域汇总(求和、平均、计数)到一个自动新建的“汇总”工作薄中。工作与工作一一对应,单元格与单元格一一对应汇总(求和、全部求和、平均、计数)数据。。 8、多汇总——求和(默认)、全部求和、平均、计数:将某一工作薄中所有工作数据,按照所选择的单元格区域进行汇总(求和、平均、计数)。 9、插入空行:在当前单元格下隔行插入空行。 10:删除空行:删除当前单元格以下的所有空行或0值行。 11、插入头:在所选择的头区域下方,隔行插入头(工资条)。 12、批量打印:打印某一文件夹(或当前文件夹)下的所有工作薄,可以选择打印“整个工作薄”、按名称“选定工作”、按位置“指定工作”,可选择“打印份数”、“逐份打印”。 二、注意: “多薄合并、多合并”:要合并的工作薄(或工作)格式必须一致;工作一一对应;所有工作头行数必须相同;仅复制头以下的行;多薄多合并,具有“去除重复行”功能,即一行的各列数据均相同的行仅保留一行;工作下方不要有备注行。 三、安装使用: 1、 安装时可能受360警告或拦截,这是正常提示,选择“添加信任”或“直接运行”即可。 安装完成后,在excel203工具栏或excel2010“多薄多”选项卡中会有“多薄合并、多合并、清除数据、分类汇总、拆分格、选择查询”等功能按钮。(见下图) 2、默认安装位置为C:\WINDOWS\mergebooks.dll ; 如果电脑进行了多用户设置,如电脑用户为lcb,可把mergebooks.dll复制到C:\Documents and Settings\lcb\Application Data\Microsoft\AddIns\mergebooks.dll 中。 3、Excel2003调出“多薄多”方法: 如果在excel2003工具栏中没有以上按钮,则按如下方法进行: 第一步(添加到COM加载项):打开excel,在“工具栏”上点击“COM加载项”。调出如下窗口。 把“可用加载项”中的“mergebooks.dll” (打勾),此时excel2003工具栏中将出现按钮;如果没有出现,则继续点击上图中的“添加”按钮,进入以下窗口: 在右边窗口中选择Mergebooks.dll——确定——返回(第一个窗口)——(继续)确定返回——关闭excel。 第二步(添加到工具栏):再次打开excel2003,点击“COM加载项”进入第一个窗口,把左边窗口中的“Mergebooks”(打勾)——确定(返回),即可出现在工具栏中。 4、Excel2010调出“多薄多”方法:文件——选项——自定义功能区——(右边)“多薄多”(打勾) 开发者:[email protected]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值