简单查询

  • 1.查询特定的列
  • 2.查询所有的列
  • 3.给列起别名(简化列名称)
  • 4.显示不同的记录
  • 5.查询时执行计算
  • 6.查询结果排序
  • 7.条件查询 where
  • 8.> < >= <= = !=(不等于) is (not) null && ||
  • 9.模糊条件查询like


以下查询练习以下表emp(人员表)和dept(部门表)为例

mysql怎么查询员工工资 mysql查询员工信息_mysql怎么查询员工工资


mysql怎么查询员工工资 mysql查询员工信息_mysql怎么查询员工工资_02

1.查询特定的列

查询出所有员工的编号和姓名
select eid,ename from emp;

查询出所有员工的姓名、生日、工资
select ename,birthday,salary from emp;

2.查询所有的列

select eid,ename,sex,birthday,salary,deptId from emp;select * from emp;

3.给列起别名(简化列名称)

查询出所有员工的编号和姓名,使用汉字别名
select eid as 编号,ename as 姓名 from emp;

查询出所有员工的姓名、性别、生日、工资,使用一个字母作为别名
select ename a,sex b,birthday c,salary d from emp; tip:as关键字可以省略,保留即可。

4.显示不同的记录

查询出都有哪些性别的员工
select distinct sex from emp;

查询出员工都分布在哪些部门
select distinct deptId from emp;

5.查询时执行计算

计算 1+3+5+87.4+54.39
select 1+3+5+8*7.4+5*4.39;

查询出所有员工的姓名及其年薪
select ename,salary*12 from emp;

假设每个员工的工资增长1000,年终奖20000,查询出所有员工的姓名及其年薪,使用汉字别名
select ename 姓名,(salary+1000)*12+20000 年薪 from emp;

6.查询结果排序

  • asc 升序 #ascendant 升序的
  • desc 降序 #descendant 降序

查询出所有的部门,结果按照部门编号升序排列
select * from dept order by did asc;

查询出所有的部门,结果按照部门编号降序排列
select * from dept order by did desc;

查询出所有的员工,结果按照工资降序排列
select * from emp order by salary desc;

查询出所有的员工,结果按照年龄从大到小排列(生日从小到大)
select * from emp order by birthday asc;

查询出所有的员工,结果按照姓名的升序排列
select * from emp order by ename;tip:按照字符串排序是按照字符的编码排列
不加排序规则,默认是按照升序排列

查询出所有的员工,结果按照工资的升序排列,要求男员工显示在前女员工显示在后
select * from emp order by sex desc, salary;

查询出所有的员工,结果按照部门升序排列,如果部门相同按照年龄从小到大排列(生日越小年龄越大)
select * from emp order by deptId,birthday desc;

7.条件查询 where

查询出编号为5的员工所有列
select * from emp where eid=5;

查询出姓名为king的员工所有列
select * from emp where ename='king';

查询出20号部门下的员工有哪些
select * from emp where deptId=20;

查询出工资在6000以上的员工有哪些
select * from emp where salary>6000;

8.> < >= <= = !=(不等于) is (not) null && ||

and (&&) 并且,两个条件都满足
or (||) 或者,两个条件满足其一
is null 值为null
is not null 值不为null
in( ) 满足其中一个
not in( ) 都不满足


查询出不在20号部门下的员工有哪些

select * from emp where deptId!=20;

查询出没有明确部门的员工有哪些
select * from emp where deptId is null;

查询出有明确部门的员工有哪些
select * from emp where deptId is not null;

查询出工资在7000以上的男员工有哪些
select * from emp where salary>7000 and sex=1;select * from emp where salary>7000 && sex=1;


a between and b 在a~b之间

a not between and b 不在a~b之间

查询出工资在6000~9000直接的员工有哪些

select * from emp where salary>=6000 && salary<=9000;select * from emp where salary between 6000 and 9000;

查询出工资在6000以下或者9000以上的员工有哪些
select * from emp where salary<6000 or salary>9000;select * from emp where salary<6000 || salary>9000;select * from emp where salary not between 6000 and 9000;

查询出1993年出生的员工有哪些
select * from emp where birthday>='1993-1-1' && birthday<='1993-12-31';

select * from emp where birthday between '1993-1-1' and '1993-12-31';

查询出20号部门或者30号部门的员工有哪些
select * from emp where deptId=20 || deptId=30;select * from emp where deptId in(20,30);

查询出不在20号部门并且不在30号部门的员工有哪些
select * from emp where deptId!=20 && deptId!=30;select * from emp where deptId not in(20,30);


9.模糊条件查询like

%——匹配任意个字符 >=0
_ ——任意一个字符 =1

查询出姓名中含有字母o的员工有哪些
select * from emp where ename like '%o%';

查询出姓名中以o结尾的员工有哪些
select * from emp where ename like '%o';

查询出姓名中第二个字符是o员工有哪些
select * from emp where ename like '_o%';