MySQL数据查询操作

表名为 student dept
列名为  empon  ename job mgr hiredate sal comm deptno  dname

单表查询
(1)查询表中的全部数据
     select * from student;
(2)查询表中的部分数据
     <部分列,查询所有员工的编号,姓名,薪水>
     select empno,ename,sal from student;

     <部分行,查询薪水大于2500的员工信息>
     select * from student where sal>2500;

     <部分行和列,查询薪水大于2500的员工编号,姓名,薪水>
     select empon,ename,sal where sal>2500;
(3)查询结果的排序
     select * from student order by sal asc;   ------>>>按照升序对表进行排序
     select * from student order by sal desc;   ------->>>按照降序对表进行排序
(4)模糊查询
   a.  like  像
     <查询员工姓名中含有 M 的员工信息>
     select * from student where ename like '%M%';
     <查询员工姓名中以 M 开头的员工信息>
     select * from student where ename like 'M%';
     <查询员工姓名中第二个字母是 M 的员工信息>
     select * from student where ename like '_M%';
   b.  区间范围    between...and
     <查询员工薪水在2000到3000之间的员工信息>
     select * from student where sal between 2000 and 3000;
   c.在指定值中进行选择   in
     <查询职位为 'CLERK','MANAGER' 的员工信息>
     select * from student where job in ('CLERK','MANAGER') order by job;   ------>>>默认升序排序
   d.使用带函数的查询员工信息(分组函数/聚合函数)
     <查询一共有多少个员工>
     select count(*) from student;
     <查询20号部门有多少人>
     select count(*) from student where depton=20;

     <查询员工的总薪水>
     select sum(sal) from student;
     <查询20号部门的总薪水>
     select sum(sal) from student where depton=20;
     <查询员工的平均薪水>
     select avg(sal) from student;
     <查询20号部门员工的平均薪水>
     select avg(sal) from student where deptno=20;
     <求员工表当中的最高工资>   --------------->>>>>>><求员工最低工资用 min(sal) >
     select max(sal) from student;
     <求20号部门的员工最高工资>
     select max(sal) from student where deptno=20;

(5)分组查询:   group by...having   与分组函数 count  max  min avg sum 一起使用
   <查询每个部门的总人数>
   select deptno,count(*) from student group by deptno;
   <查询每个部门总人数不低于4人的部门编号以及部门总人数>
   select deptno,count(*) from student group by deptno having count(*)>3;

(6)表链接查询    99标准的表连接查询
   <查询薪水大于2000的员工编号,姓名,薪水,员工的部门名称并且按照薪水降序排序> ----->>>内连接查询
   select empno,ename,sal,dname from student inner join dept on student.deptno=dept.deptno where sal>2000 order by sal desc;
   <查询所有部门员工的编号,姓名,薪水,部门名称,包含没有员工的部门>
   select empno,ename,sal,dname from student left join dept on student.deptno=dept.deptno; --->>>以右边为主表
   select empno,ename,sal,dname from student right join dept on student.deptno=dept.deptno; --->>>以左边为主表

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

@苇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值