Mysql(二)——简单查询及示例

以下查询练习以下表emp(人员表)和dept(部门表)为例
在这里插入图片描述
在这里插入图片描述

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%';

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你脸上有BUG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值