-- 继续mysql自我练习基础1, 数据和表格沿用基础1
-- 单表查询(虚拟结果集)
select * from emp; -- 检查表emp所有字段数据内容
-- 查询指定列:查询emp表中ename,job,sal
select ename,job,sal from emp;
-- 设置别名:查询每位员工调整后的薪资(基本工资+1000)
select *,sal+1000 as 调薪 from emp;-- as 也可以省略
select *,sal+1000 "调 薪" from emp;-- 别名为中文字符中间有空格时或者别名为纯数字时必须用引号
-- 练习:查询每位员工的年薪(基本工资*12):empno,ename,年薪
select empno,ename,sal*12 年薪 from emp; -- 查询年薪
-- 查询emp表中每个部门有哪些职位
select distinct deptno, job from emp;-- 将distinct放在查询去重的字段前面
-- 条件查询
-- 查询10号部门和20号部门中sal低于2000的员工信息
select * from emp where (deptno=10 or deptno=20) and sal<2000;
select * from emp where deptno in(10,20) and sal<2000;-- 方法2 用in作为条件
-- 练习:查询基本工资大于等于2000小于等于3000的员工信息
select * from emp where sal>=2000 and sal<=3000;
select * from emp where sal between 2000 and 3000;-- 方法二 包含2最小值和最大值,时间也可以用between and
select * from emp where sal in(2000,3000);-- 查询的是薪酬等于2000或者等于3000的
-- 空值查询
-- 查询mgr为空的记录
select * from emp where mgr=null;-- 错误方式,因为不知道控制是多少,所有无法找到
select * from emp where mgr is null;-- 正确方式,用is null 判断是不是空
-- 练习:查询comm不为空的记录
select * from emp where comm is not null;
-- 模糊查询
-- 查询姓名以a开头的员工信息
select * from emp where ename like 'a%';
-- 查询姓名中包含a的员工信息
select * from emp where ename like '%a%';
-- 查询姓名中第二个字符为a的员工信息
select * from emp where ename like '_a%';
-- 练习:查询员工姓名中不包含s的员工信息
select * from emp where ename not like '%s%';
-- 查询结果排序
-- 单字段排序:查询所有员工信息按sal降序显示
select * from emp order by sal desc;
-- 多字段排序:查询所有员工信息按deptno升序、sal降序显示
select * from emp order by deptno asc, sal desc;
select * from emp order by deptno, sal desc;-- 方法二asc可以省略不写默认升序
-- 限制查询结果数量
-- 查询基本工资最高的前5位员工
select * from emp order by sal desc limit 5;
select * from emp order by sal desc limit 0, 5;-- 偏移量为0时可以省略偏移量
-- 查询基本工资第6到10名的员工
select * from emp order by sal desc limit 5,5;-- limit 偏移量,返回总行数
-- 练习:查询最后入职的5位员工
select * from emp order by hiredate desc limit 5;--