码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【MYSQL】复合查询


    文章目录

    • 基本查询
      • 查询工资高于500 或 岗位是MANGER的雇员,同时还要满足他们的姓名首字母为大写J
      • 按照部门号升序、工资降序排序
      • 使用年薪降序排序
      • 显示工资最高的员工的名字和工作岗位
      • 显示工资高于平均工资的员工信息
      • 显示每个部门的平均工资和最高工资
      • 显示平均工资低于2000的部门号和它的平均工资
      • 显示每种岗位的员工总数,以及平均工资
    • 多表查询
      • 显示雇员名、雇员工资、以及员工所在部门的名字
      • 显示各个员工的姓名、工资、工资级别
      • 自连接
      • 显示员工FORD的上级领导的编号和姓名
    • 子查询
      • 单行子查询
        • 显示与SMITH同一部门的员工
      • 多行子查询
        • 查询和10号部门的工作岗位相同的雇员的名字、岗位、工资、部门号,但不包含10号自己的
        • 显示工资比部门30号 所有员工的工资高的员工的姓名、工资、和部门号
      • 显示工资比30号部门的任意员工的工资高的员工的姓名工资和部门号
      • 多列子查询
        • 查询和SMITH部门和岗位完全相同的雇员,不含SMITH本人
      • 在from语句中使用子查询
        • 显示每个高于自己部门平均工资的员工的姓名、部门、工资、平均工资
        • 查找每个部门工资最高的人的姓名、工资、部门、最高工资
        • 显示每个部门的信息(部门名、编号、地址)和人员数量
      • 合并查询
        • union
        • 将工资大于2500 或 职位是MANAGER的人找出来
        • union all

    基本查询

    查询工资高于500 或 岗位是MANGER的雇员,同时还要满足他们的姓名首字母为大写J

    select ename from emp where (sal > 500 OR job = 'MANGER') and substring(ename,1,1) = 'J';    
    
    • 1

    按照部门号升序、工资降序排序

    select * from emp order by deptno asc,sal desc;
    
    • 1

    使用年薪降序排序

    select sal*12+ifnull(comm,0) as sum from emp order by sum desc;
    
    • 1

    显示工资最高的员工的名字和工作岗位

    select * from emp where sal = (select max(sal) from emp);
    
    • 1

    显示工资高于平均工资的员工信息

    select * from emp where sal > (select avg(sal) from emp);
    
    • 1

    显示每个部门的平均工资和最高工资

    select deptno,format(max(sal),2) 最高 , format(avg(sal),2) 平均 from emp group by deptno;
    
    • 1

    显示平均工资低于2000的部门号和它的平均工资

    select deptno,avg(sal) 平均 from emp group by deptno having 平均 > 2000;
    
    • 1

    显示每种岗位的员工总数,以及平均工资

    select job,count(*) 人数,avg(sal) 平均工资 from emp group by job;
    
    • 1

    多表查询

    显示雇员名、雇员工资、以及员工所在部门的名字

    两张表进行笛卡尔积。即用第一张表的一条数据和第二张表的所有数据进行组合。

    select emp.ename,sal,dname from emp,dept where emp.deptno=dept.deptno;
    
    • 1

    显示各个员工的姓名、工资、工资级别

    select ename,sal,grade from emp,salgrade where sal between losal and hisal; 
    
    • 1

    自连接

    select * from salgrade as t1,salgrade as t2;
    
    • 1

    显示员工FORD的上级领导的编号和姓名

    //子查询
    select mgr from emp where ename = 'FORD';
    
    select ename,emptno from emp where empno = (select mgr from emp where ename = 'FORD');
    
    //自连接
    select * from emp e1,emp e2 where e1.name='FORD' and e1.mgr=e2.empno;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    子查询

    嵌入在其他sql语句中的select语句,也叫嵌套查询

    单行子查询

    显示与SMITH同一部门的员工

    select dptno from emp where ename = 'SMITH';
    
    select * from emp where deptno=(select dptno from emp where ename = 'SMITH')
    
    
    • 1
    • 2
    • 3
    • 4

    多行子查询

    查询和10号部门的工作岗位相同的雇员的名字、岗位、工资、部门号,但不包含10号自己的

    select ename,job,sal,deptno from emp where job in   (select distinct job from emp where deptno=10) and deptno <> 10;
    
    • 1

    还要员工的部门名字:

    select * from (select ename,job,sal,deptno from emp where job in (select distinct job from emp where deptno=10) and deptno <> 10) as tmp , dept where dept.deptno=tmp.deptno;
    
    • 1

    显示工资比部门30号 所有员工的工资高的员工的姓名、工资、和部门号

    select * from emp where sal > (select max(sal) from emp where deptno=30);
    
    select * from emp where sal > all(select distinct sal from emp where deptno=30);
    
    • 1
    • 2
    • 3

    显示工资比30号部门的任意员工的工资高的员工的姓名工资和部门号

    select * from emp where sal > any(select distinct sal from emp where deptno=30);
    
    • 1

    多列子查询

    查询和SMITH部门和岗位完全相同的雇员,不含SMITH本人

    select * from emp where (dept,job) in (select deptno,job from emp where ename='SMITH') and ename <> 'SMITH';
    
    • 1

    在from语句中使用子查询

    显示每个高于自己部门平均工资的员工的姓名、部门、工资、平均工资

    //1.每个部门的平均工资
    select deptno,avg(sal) from emp group by deptno;
    
    //2.进行笛卡尔积
    select * from emp,(select deptno,avg(sal) from emp group by deptno) as tmp;
    
    //3.筛选正确的部门
    select * from emp,(select deptno,avg(sal) from emp group by deptno) as tmp where emp.deptno = tmp.deptno;
    
    //4.单表查询
    select * from emp,(select deptno,avg(sal) myavg from emp group by deptno) as tmp where emp.deptno = tmp.deptno and emp.sal > tmp.myavg;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    查看这一些人的办公地点:

    1.上面的查询结果和dept表进行笛卡尔积
    2.筛选合适的行
    3.按条件得到要求的
    
    • 1
    • 2
    • 3

    查找每个部门工资最高的人的姓名、工资、部门、最高工资

    //1.每个部门的最高工资
    select deptno,max(sal) from emp group by deptno;
    
    //2.和emp表笛卡尔积,找到谁是最高的那个人
    select ename,sal,t1.deptno,mymax from emp t1, (select deptno,max(sal) mymax from emp group by deptno) t2 where t1.deptno = t2.deptno and t1.sal = t2.mymax;
    
    //3.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    显示每个部门的信息(部门名、编号、地址)和人员数量

    • 使用子查询
    //各部门人员数量
    select deptno,count(*) dept_num from emp group by deptno;
    
    //跟dept表进行笛卡尔积
    select t1.dname,t1.loc,t2.dept_num,t1.deptno from dept t1,(select deptno,count(*) dept_num from emp group by deptno) t2 where t1.deptno=t2.deptno;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    多表查询的本质:将多表转换成单表。

    合并查询

    union

    该操作符用于取得两个结果集的并集,当使用该操作符时,会自动去掉结果集中的重复行(自动去重)。要拼接的时候要满足两个列一致,否则不能拼。

    将工资大于2500 或 职位是MANAGER的人找出来

    select * from emp where job = 'MANNAGER' union select * from emp where job = 'MANAGER';
    
    • 1

    union all

    该操作符不会对满足多个条件的数据进行去重,而是保留。

  • 相关阅读:
    django 任务管理-apscheduler
    python读取PDF文件并做词云可视化
    卷积神经网络-AlexNet
    golang gin——文件上传(单文件,多文件)
    FlinkSql+Stream综合使用+广播流
    svelte初探-上
    “机器学习”名字的由来
    2yocto 自启动程序(服务)
    查题公众号搭建方法——只需两分钟
    数据结构:二叉树的非递归遍历
  • 原文地址:https://blog.csdn.net/m0_73209194/article/details/133652693
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号