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 的结 ...
随机推荐
- Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) D mt19937
https://codeforces.com/contest/1040/problem/D 用法 mt19937 g(种子); //种子:time(0) mt19937_64 g(); //long ...
- [TJOI2019]唱,跳,rap,篮球(生成函数,组合数学,NTT)
算是补了个万年大坑了吧. 根据 wwj 的题解(最准确),设一个方案 \(S\)(不一定合法)的鸡你太美组数为 \(w(S)\). 答案就是 \(\sum\limits_{S}[w(S)=0]\). ...
- [LeetCode] 839. Similar String Groups 相似字符串组
Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it ...
- [LeetCode] 491. Increasing Subsequences 递增子序列
Given an integer array, your task is to find all the different possible increasing subsequences of t ...
- [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Golang(九)简单 Goroutine 池实现
0. 前言 最近使用 Golang 写一个并发执行的测试脚本 之前习惯使用 Java,习惯性想先建一个线程池.然后意识到 Golang 没有封装好的线程池 结合之前学习的 Goroutine 原理和 ...
- pandas的使用(7)--分组
pandas的使用(7)--分组
- ASP.NET Core 3.0 WebApi 系列【2】.Net Core 3.0+ CodeFirst + MySql 实现数据的迁移
写在前面 在前一小节中,我们创建了基于RESFULL风格的服务.这个小节,尝试使用CodeFirst+MySql 的方式实现数据迁移. 一.开发环境 [1]运行环境:win10 家庭版 [2]开发工具 ...
- 记WinForm中WebBrowser相关的俩个问题
问题一:如何不让WebBrowser中弹出“安全警告” 当链接https网址时,IE会自动弹出上图中的窗口. 关闭窗口的具体思路如下: 使用WebBrowser加载中/加载完毕后触发的事件处理程序,在 ...
- Vue.js项目实战-打造线上商城
首先上一下完成后的效果: 首页: 商品详情页: 购物车页(其实还有个订单页,只是和购物车页基本类似,所以就不截图啦): 开始项目: 由于涉及的是前后端分离,所以我们的后台数据就模拟存储于浏览器端(co ...