实验前言
今天做课题时做到了有关Mysql的有关内容,希望能帮到一些人(* ̄︶ ̄)
我利用链接提供了本次实验的三种数据内容
详情查看“”实验数据“”
提取码zcyx
实验准备
需要Navicat for MySQL(我的版本比较低)和三钟数据内容
1.首先将Mysql打开——>点击左上链接——>输入账号和密码都是root——>点击确定
2.在root中建立一个新的数据库——>将下载好的三钟数据文件鼠标右键复制(ctrl c)——>在新建的数据库名称中右键(ctrl v)
3.之后在新建的数据库名中鼠标右键点击刷新,之后就能发现数据出现了
实验内容
(1).掌握select语句的基本语法
(2)掌握子查询的表示
(3)掌握连接查询的表示
(4)掌握group by、order by、limit语句的使用
1.用select语句查询departments和salary表中所有的数据信息
select departments.*,salary.*
from departments,salary
实验结果
2.用select语句查询employees表中每个雇员的地址和电话
SELECT address,phonenumber
from employees码片
实验结果
3.用select语句查询departments和salary表的一列或若干列
select departmentname
from departments
实验结果
4.查询employees表中的部门编号和性别,要求使用distinct消除重复行
select distinct departmentID,sex
from employees
实验结果
5.查询employeeID为000001的雇员的地址和电话
select address,phonenumber
from employees
where EmployeeID='000001'
实验结果
6.查询月收入高于2000元的员工编号
SELECT employeeID
from salary
where InCome>2000
实验结果
7.查询1970年以后出生的员工的姓名和住址
SELECT name,address
from employees
where Birthday>'1970-01-01'
实验结果
8.查询employees表中女雇员的住址和电话,使用as子句将结果中各列的标题分别指定为住址、电话
SELECT address as '住址',phonenumber as '电话'
from employees
where sex=0
实验结果
9.查询employees表中男员工的姓名和出生日期,要求将各列标题用中文表示
select name as '姓名',birthday as '出生日期'
from employees
where sex=1
实验结果
10.查询employees表中员工的姓名和性别,要求sex值为1时显示为“男”,为0时显示为“女”
SELECT name '姓名',
case
when sex =1 then'男'
when sex=0 then'女'
end as '性别'
from employees
实验结果
11.查询employees员工的姓名、住址、和收入水平,2000元以下为显示为低收入,2000~3000元显示为中等收入,3000元以上显示为高收入
select Name as 姓名,Address as 住址,Income=
CASE
when Income<2000 then'低收入'
when Income<3000 then'中等收入'
when Income>3000 then'高收入'
END
from employees,salary
where employees.EmployeeID=salary.EmployeeID
实验结果
12.计算每个雇员的实际收入
SELECT employeeID,'实际收入'=InCome-outcome
from salary
实验结果
13.计算salary表中员工月收入的平均数
SELECT AVG( Income)
from salary
实验结果
14.获得employees表中年龄最大的员工编号
SELECT employeeID
from employees
where Birthday =(SELECT min(Birthday)
from employees)
实验结果
15.计算salary表中所有员工的总支出
select sum(outcome)as'总支出'
from salary
实验结果
16.查询财务部雇员的最高和最低实际收入
SELECT MAX(income-outcome),MIN(income-outcome)
from salary,employees
where salary.EmployeeID=employees.EmployeeID and DepartmentID=1
实验结果
17.找出所有姓王的雇员的部门编号
SELECT departmentID
from employees
where name like'王%'
实验结果
18.找出所有其住址中含有“中山”的雇员的员工编号及部门编号
SELECT employeeID as'员工编号',departmentID as'部门编号'
from employees
where Address like'%中山%'
实验结果
19.查找员工编号中倒数第二个数字为“0”的员工的姓名、住址、和学历
SELECT name '姓名',address '住址',education '学历'
from employees
WHERE EmployeeID LIKE'%0_'
实验结果
20.找出所有收入在2000~3000元之间的员工编号
SELECT employeeID
from salary
where income BETWEEN 2000 and 3000
实验结果
21.找出所有部门“1”或“2”工作的雇员的员工编号
SELECT employeeID
from employees
where DepartmentID=1 or DepartmentID=2
实验结果
子查询
1.查找在财务部工作的雇员的情况
SELECT *
from employees
where DepartmentID =
(
SELECT DepartmentID
from departments
where DepartmentName='财务部'
)
实验结果
2.用子查询的方法查找所有收入在2500元以下的雇员的情况
SELECT *
from employees
where EmployeeID IN
(
SELECT EmployeeID
from salary
where InCome<2500
)
实验结果
3.用子查询的方法查找研发部比市场部所有雇员收入都高的雇员的姓名
SELECT Name
from employees,salary
where employees.EmployeeID=salary.EmployeeID
AND
DepartmentID IN
(
select DepartmentID
from departments
where DepartmentName='研发部'
)
AND
InCome>ALL
(
SELECT InCome
from salary
where DepartmentID IN
(
SELECT DepartmentID
from departments
WHERE DepartmentID='市场部'
)
)
实验结果
4.用子查询的方法查找所有年龄比研发部雇员年龄都大的雇员的姓名
SELECT NAME
from employees
where Birthday<=ALL
(
SELECT Birthday
from employees
where DepartmentID=
(
SELECT DepartmentID
from departments
where departmentname='研发部'
)
)
实验结果
连接查询
1.使用内连接的方法查找不在市场部工作的所有雇员信息
SELECT *
From employees JOIN departments ON employees.DepartmentID=departments.DepartmentID
where DepartmentName!='市场部'
实验结果
2.使用外连接方法查找所有雇员的月收入
select Income
from employees LEFT OUTER JOIN salary ON employees.EmployeeID=salary.EmployeeID
实验结果
3.查询研发部在1970年以前出生的雇员姓名及其薪水详情
SELECT name,income,outcome
from employees,salary,departments
WHERE employees.EmployeeID=salary.EmployeeID
and employees.DepartmentID=departments.DepartmentID
and DepartmentName='研发部'
and Birthday<'1970-01-01'
实验结果
4.使用外连接的方法查找出所有员工的月收入
SELECT income
from employees LEFT OUTER JOIN salary on employees.EmployeeID=salary.EmployeeID
实验结果
5.使用内连接的方法查找出不在财务部工作的所有员工信息
SELECT *
from employees JOIN departments ON employees.DepartmentID=departments.DepartmentID
where DepartmentName!='财务部'
实验结果
6.使用内连接的方法查询名字为“王林”的员工所在的部门
SELECT departmentName
from departments JOIN employees ON departments.DepartmentID=employees.DepartmentID
where employees.Name='王林'
实验结果
聚合函数(group by、order by和limit子句的使用)
1.按雇员的学历分组,列出本科、大专和硕士的人数
SELECT education,COUNT(education) 人数
from employees
GROUP BY education
实验结果
2.查询雇员姓名、性别和工龄信息,要求按实际收入从大到小排列
SELECT name as'姓名',sex as'性别',workyear as'工龄'
from employees,salary
where employees.EmployeeID=salary.EmployeeID
order by InCome DESC
实验结果
3.返回Employees表中从第3位雇员开始的5个雇员的信息
SELECT * from employees
LIMIT 2,3
实验结果
4.将员工信息按出生日期从小到大排列
SELECT *
FROM employees
ORDER BY Birthday DESC
实验结果
5.按员工的工作年份分组,统计各个工作年份的人数
SELECT workyear,COUNT( workyear)'人数'
from employees
GROUP BY Education
实验结果
总结
由于我的软件版本是旧版本的所以提醒一下,第一行代码 as ‘名称’ 可以直接写‘名称’不用写as
(* ̄︶ ̄)
如果有问题欢迎大家指正