mysql 单表,多表,符合条件,子查询
单表:
HAVING过滤
二次筛选 只能是group by 之后的字段
1.查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数
select post,group_concat(name),count(1) from employee group by post having count(1)<2;
2. 查询各岗位平均薪资大于10000的岗位名、平均工资
select post,avg(salary) as a from employee group by post having a >10000;(怕avg为关键字,用as a 代替)
3. 查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资
select post,avg(salary) as a from employee group by post having a>10000 and a<20000;
order by 查询排序
排序 order by age desc / asc
1. 查询所有员工信息,先按照age升序排序,如果age相同则按照hire_date降序排序
select * from employee order by age asc,hire_date desc;
2. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资升序排列 select post,avg(salary) from employee group by post having avg(salary)>10000 order by avg(salary) asc; (order by 后面一定要有参数)
limit 限制查询的记录数:
select * from employee order by salary desc limit 5,5; (默认为0
从排好序的第五条开始,找5个)
多表查询
- 多表连接查询
select * from employee,department where employee.dep_id = department.id
外链接操作:
1.语法
inner /left/right join (显示全部 有nul不显示,优先左边,没有关联的表为null/右边优先)
select * from employe inner join department on employee.dep_id = department.id
全外链接 union
select * from employee left join department on employee.dep_id = department.id
union
select * from employee right join department on employee.dep_id = department.id
符合条件查询
以内连接的方式查询employee和department表,并且employee表中的age字段值必须大于25,
即找出年龄大于25岁的员工以及员工所在的部门
select employee.name,department.name from employee inner join department on employee.dep_id = department.id where age >25;
(on 后面跟着where 条件)
1、查询平均年龄在25岁以上的部门名
select name from department where id in( select dep_id from employee group by dep_id having avg(age) >25) ;
通过在emp 表中找到的dep_id 对应上 dep 表中的id 而找到name
2、查看不足1人的部门名
select name from department where id not in(select dep_id from employee group by dep_id) ;
(利用共同的dep_id来判断)
3、查询大于所有人平均年龄的员工名与年龄
select name,age from employee where age> (select avg(age) from employee);
(这里找的是员工名和年龄 跟部门没有关系,,,)
4、查询大于部门内平均年龄的员工名、年龄
1)先对员工表(employee)中的人员分组(group by),查询出dep_id以及平均年龄。
(2)将查出的结果作为临时表,再对根据临时表的dep_id和employee的dep_id作为筛选条件将employee表和临时表进行内连接。
(3)最后再将employee员工的年龄是大于平均年龄的员工名字和年龄筛选。
select * from employee inner join (select dep_id,avg(age) as b from employee group by dep_id) as A on employee.dep_id = A.dep_id where employee.age>A.b;
5.查询每个部门最新入职的那位员工
select name from employee inner join( select post,max(hire_date) as newtime from employee group by post) as A on employee .post = A.post where employee.hire_date=A.newtime;
同上,都是关于进行 on dep_id 的判断 ,第二个则是关于where 的判断
pymysql模块的使用
mysql 单表,多表,符合条件,子查询的更多相关文章
- MySQL之多表查询一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习
MySQL之多表查询 阅读目录 一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习 一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 首先说一下,我们写项目一般都会建 ...
- Mysql常用sql语句(20)- 子查询重点知识
测试必备的Mysql常用sql语句系列 https://www.cnblogs.com/poloyy/category/1683347.html 子查询语句可以嵌套在 sql 语句中任何表达式出现的位 ...
- Mysql 多表数据拼接插入及子查询结果集随机取一条
最近遇到一个测试数据的需求,需要往一个表中插入4个来源的数据. 往orders 表中插入 来自 sql_person cm_user_car_model cm_sp_product_new 部分固定数 ...
- mysql基础教程(三)-----增删改、子查询、创建管理表、约束和分页
插入 INSERT语句语法 从其它表中拷贝数据 • 不必书写 VALUES 子句. • 子查询中的值列表应与 INSERT 子句中的列名对应 update语句 • 可以一次更新多条数据. • 如果需要 ...
- MySQL中的DML、DQL和子查询
一.MySQL中的DML语句 1.使用insert插入数据记录: INSERT INTO `myschool`.`student` (`studentNo`, `loginPwd`, `student ...
- 【MySQL】-2 函数、分组、子查询、联合查询
函数 Mysql的函数特性没有SQL可移植性强. 大多数情况下支持的函数: 处理文本串的函数: RTrim():处理列值右边的空格 LTrim():处理列值左边的空格 Trim():处理列值的左右 ...
- Mysql高手系列 - 第12篇:子查询详解
这是Mysql系列第12篇. 环境:mysql5.7.25,cmd命令中进行演示. 本章节非常重要. 子查询 出现在select语句中的select语句,称为子查询或内查询. 外部的select查询语 ...
- MySQL数据库基础(四)(子查询与链接)
1.子查询简介 其中,所谓的"外层查询"并不是指"查找",指的是所有SQL语句的统称:结构化查询语言(Structured Query Language),简称 ...
- MySQL数据库(10)----IN 和 NOT IN 子查询
当子查询要返回多个行来与外层查询进行比较运算时,可以使用运算符 IN 和 NOT IN.它们会测试某个给定的比较值是否存在于某一组值里.如果外层查询里的行与子查询返回的某一个行相匹配,那么 IN 的结 ...
随机推荐
- IAR环境搭建
工具下载:https://pan.baidu.com/s/1nwv0RVz 第一步:右键点击EW8051-EV-8103-Web.exe,使用管理员权限运行. 第二步:我们运行之后只要一直Next下去 ...
- [LeetCode] 904. Fruit Into Baskets 水果装入果篮
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your cho ...
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [LeetCode] 48. Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- ansys meshing划分无厚度的面
优酷播放地址: https://v.youku.com/v_show/id_XNDQ3MjAyODYzMg==.html?spm=a2hzp.8244740.0.0 原版视频教程下载地址: https ...
- FWT-快速沃尔什变换
FWT-快速沃尔什变换 FWT有啥用啊 我们知道,FFT可以解决多项式的卷积,即 \[ C_k=\sum_{i+j=k}A_i*B_j \] 如果将操作符换一下,换成集合运算符 比如 \[ C_k=\ ...
- Oracle常用函数集锦
1.wmsys.wm_concat函数 将列转为行.例子: --表里的数据如下 SQL> select * from idtable; ID NAME ---------- ---------- ...
- 大话设计模式Python实现-代理模式
代理模式(Proxy Pattern):为其他对象提供一种代理以控制对这个对象的访问 #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = ...
- Debug 路漫漫-11:Python: TypeError: 'generator' object is not subscriptable
调试程序,出现以下错误: Python: TypeError: 'generator' object is not subscriptable “在Python中,这种一边循环一边计算的机制,称为生成 ...
- 浙大版《C语言程序设计(第3版)》题目集 --总结
浙大版<C语言程序设计(第3版)>题目集 此篇博客意义为总结pta上浙大版<C语言程序设计(第3版)>题目集所做题目的错误点,心得体会. 1.练习2-10 计算分段函数[1] ...