mysql子查询复杂查询

课程名称

MySQL数据库技术

实验

成绩

 

实验名称

实验五:复杂查询

学号

 辅导老师;陶荣

姓名

 

班级

 

日期

 

实验目的:

1.掌握select语句的基本语法;

2.掌握连接查询和子查询的使用方法;

实验平台:

MySQL+SQLyog;

实验内容与步骤:

以下操作均在YGGL数据库中进行。

一、子查询练习:

1. 查找在财务部工作的员工的情况。

 

2. 使用子查询的方法查找所有收入在2500元以下的员工的情况。

 

3. 查找研发部年龄不低于市场部所有员工年龄的雇员姓名。

 

4. 用子查询的方法查找研发部比市场部所有雇员收入都高的雇员的姓名。

 

5. 查找比广告部所有员工收入都高的员工的姓名。

 

6. 在order by中使用子查询,查询员工姓名、性别和工龄信息,要求按

实际收入从大到小排列。

 

二、连接查询练习:

1. 查询每个员工的情况及其薪水的情况。

 

2. 使用内连接的方法查询名字为“王林”的员工所在的部门。

 

3. 使用内连接方法查找不在广告部工作的所有员工信息。

 

4. 使用外连接方法查找所有员工的月收入。

 

5. 查找广告部收入在2000元以上的雇员姓名及其薪水详情。

 

6. 查询研发部在1966年以前出生的员工姓名及其薪水情况。

 

 

实验总结(结论或问题分析):

      select * from `employees`  where 部门号 IN(select 部门号 FROM departments where 部门号=1);

 

select * from employees e,`salary`s

where e.编号=s.编号 and 收入<2500

 

select 姓名  from employees e,departments d

where 出生日期>=all(SELECT 出生日期 from employees e, departments d

where e.部门号=d.部门号 and d.部门名称='财务部')

and e.部门号=d.部门号 AND d.部门名称='研发部';

 

select 姓名  from employees e,departments d,salary s

where 收入>all(select 收入 FROM employees e,departments d, salary s

where e.部门号=d.部门号 and s.编号=e.编号 and d.部门名称='市场部')

 and e.部门号=d.部门号 and s.编号=e.编号 and d.部门名称='研发部';

 

 

SELECT 姓名 from `employees` e where 部门号 in(

SELECT 部门号 FROM salary s where 

 收入>=all (select 收入 from salary where 编号 

 IN(select 编号 from employees e where  部门号=(select 

 部门号 from departments where 部门名称='研发部'))));

 

 SELECT 姓名,性别 from employees e,`salary`s

 where s.编号=e.编号 ORDER BY 收入 desc;

 

select * from employees e,salary s 

where s.编号=e.编号;

 

select 部门名称 from employees e 

inner join departments d  on e.部门号=d.部门号

where e.姓名='王加';

 

select* from employees e 

inner join departments d  on e.部门号=d.部门号

where d.部门号!='广告部';

 

select * from employees e left outer join salary s 

on s.编号=e.编号;

 

 

select * from employees e inner join salary s 

on s.编号=e.编号 inner join departments d on  

e.部门号=d.部门号 where s.收入>2000;

 

 

select * from employees e inner join salary s 

on s.编号=e.编号 inner join departments d on  

e.部门号=d.部门号 where e.出生日期>'1966' and d.部门名称='研发部';

 

 

 

 

 

 

        

 

 

 

     

 

 

 

            

 

 

 

 

 

 

 

 

 

 

 

 

            

            

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值