员工信息的查询——简单表查询(二)
表信息查看
——>表信息
简单查询
- 查询所有的职位
select distinct job from emp;
查询出来的job是所有人的职位,用distinct去重复
- 查询emp表中每个员工的姓名和年薪
select ename,(sal+ifnull(comm,0))*12 as salary from emp;
限定查询
- 查询工资高于1500的所有员工的信息
select * from emp where sal>1500;
- 查询工资高于(等于)1500并且低于(等于)3000的所有员工信息
select * from emp where sal>=1500 and sal<=3000;
或select * from emp where sal between 1500 and 3000;
- 查询每个月可以获得奖金的员工信息
select * from emp where comm is not null and comm!=0;
- 查询工资高于1500,同时可以获得奖金的员工信息
select * from emp where sal>1500 and comm is not null and comm!=0;
- 查询在1981年入职的员工信息
select * from emp where hiredate between '1981-01-01' and '1981-12-31';
- 查询员工名字中带S的员工信息
select *from emp where ename like '%s%';
- 查询员工姓名以H结尾的长度为5个字符的员工信息(模糊查询)
select * from emp where ename like '____H'; (四个下划线)
- 查询员工编号在7369,7788,7900的员工信息
select * from emp where empno in(7369,7788,7900);
- 查询出20部门的员工信息,查询出的信息按照工资由高到低排序,如果工资相同,按照入职日期由早到晚排序
select * from emp where deptno=20 order by sal desc,hiredate;
#order by 排序 desc倒序 asc正序,一般默认正序,可不写。
基本语法
SELECT DISTINCT 字段1,字段2... FROM 表名
WHERE 条件
GROUP BY field
HAVING 筛选
ORDER BY field
LIMIT 限制条数
- from:找到表
- where:拿着where指定的约束条件,去文件/表中取出一条条记录
- group by:将取出的一条条记录进行分组group by,如果没有group by,则整体作为一组
- select:执行select
- distinct:去重
- having:将分组的结果进行having过滤
- order by:将结果按条件排序:order by
- limit:限制结果的显示条数
基础表的查询一般比较简单,多加练习就可以掌握,建议根据博主的文首链接点进去在自己的MySQL上建表练习!!!!
喜欢的可以收藏学习!!!!