一、 单表查询   

  一 语法

select distinct 查询字段1,查询字段2,。。。 from 表名
where 分组之前的过滤条件
group by 分组依据
having 分组之后的过滤条件
order by 排序字段
limit 显示的条数;

伪代码 模仿执行过程

二 where过滤

select id,name from db39.emp where id >= 3 and id <= 6
select * from db39.emp where id between 3 and 6;

select * from emp where salary = 20000 or salary = 18000 or salary = 17000;
select * from emp where salary in (20000,18000,17000);

要求:查询员工姓名中包含i字母的员工姓名与其薪资
select name,salary from db39.emp where name like '%i%'

要求:查询员工姓名是由四个字符组成的的员工姓名与其薪资
select name,salary from db39.emp where name like '____';
select name,salary from db39.emp where char_length(name) = 4;

select * from db39.emp where id not between 3 and 6;
select * from emp where salary not in (20000,18000,17000);

要求:查询岗位描述为空的员工名与岗位名
select name,post from db39.emp where post_comment is NULL;
select name,post from db39.emp where post_comment is not NULL;

三 group by分组

#设置sql_mode为only_full_group_by,意味着以后但凡分组,只能取到分组的依据
mysql> set global sql_mode="strict_trans_tables,only_full_group_by";

#每个部门的最高工资
select post,max(salary) from emp group by post;
select post,min(salary) from emp group by post;
select post,avg(salary) from emp group by post;
select post,sum(salary) from emp group by post;
select post,count(id) from emp group by post;

#group_concat(分组之后用)
select post,group_concat(name) from emp group by post;
select post,group_concat(name,"_SB") from emp group by post;
select post,group_concat(name,": ",salary) from emp group by post;
select post,group_concat(salary) from emp group by post;

# 补充concat(不分组时用)
select name as 姓名,salary as 薪资 from emp;

select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪资 from emp;

# 补充as语法
mysql> select emp.id,emp.name from emp as t1; # 报错
mysql> select t1.id,t1.name from emp as t1;

# 查询四则运算
select name,salary*12 as annual_salary from emp;

分组练习

1. 查询岗位名以及岗位包含的所有员工名字
select post,group_concat(name) from emp group by post;

2. 查询岗位名以及各岗位内包含的员工个数
select post,count(id) from emp group by post;

3. 查询公司内男员工和女员工的个数
select sex,count(id) from emp group by sex;

4. 查询岗位名以及各岗位的平均薪资
select post,avg(salary) from emp group by post;
5. 查询岗位名以及各岗位的最高薪资
6. 查询岗位名以及各岗位的最低薪资
7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
select sex,avg(salary) from emp group by sex;

8、统计各部门年龄在30岁以上的员工平均工资
select post,avg(salary) from emp where age >= 30 group by post;

四 having过滤

having的语法格式与where一模一样,只不过having是在分组之后进行的进一步过滤
即where不能用聚合函数,而having是可以用聚合函数,这也是他们俩最大的区别

1、统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
select post,avg(salary) from emp
where age >= 30
group by post
having avg(salary) > 10000;

#强调:having必须在group by后面使用
select * from emp
having avg(salary) > 10000;

五 distinct去重

select distinct post,avg(salary) from emp
where age >= 30
group by post
having avg(salary) > 10000;

六 order by 排序

select * from emp order by salary asc; #默认升序排
select * from emp order by salary desc; #降序排

select * from emp order by age desc; #降序排

select * from emp order by age desc,salary asc; #先按照age降序排,再按照薪资升序排

# 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,
然后对平均工资进行排序

select post,avg(salary) from emp
where age > 10
group by post
having avg(salary) > 1000
order by avg(salary)
;

七 limit 限制显示条数

select * from emp limit 3;

select * from emp order by salary desc limit 1;

# 分页显示
select * from emp limit 0,5;
select * from emp limit 5,5;

八 正则表达式

select * from emp where name regexp '^jin.*(n|g)$';

二、 多表连接查询

1、内连接:把两张表有对应关系的记录连接成一张虚拟表

select * from emp inner join dep on emp.dep_id = dep.id;

#应用:
select * from emp,dep where emp.dep_id = dep.id and dep.name = "技术"; # 不要用where做连表的活

select * from emp inner join dep on emp.dep_id = dep.id
where dep.name = "技术"
;

2、左连接:在内连接的基础上,保留左边没有对应关系的记录

select * from emp left join dep on emp.dep_id = dep.id;

3、右连接:在内连接的基础上,保留右边没有对应关系的记录

select * from emp right join dep on emp.dep_id = dep.id;

4、全连接:在内连接的基础上,保留左、右边没有对应关系的记录

     full join  但是mysql没有full    解决方案如下:

select * from emp left join dep on emp.dep_id = dep.id
union
select * from emp right join dep on emp.dep_id = dep.id;

  

  补充:多表连接可以不断地与虚拟表连接

查找各部门最高工资
select t1.* from emp as t1
inner join
(select post,max(salary) as ms from emp group by post) as t2
on t1.post = t2.post
where t1.salary = t2.ms
;

5月10日 python学习总结 单表查询 和 多表连接查询的更多相关文章

  1. 4月10日 python学习总结 模块和面向对象

    1.hashlib  1.什么叫hash:hash是一种算法,该算法接受传入的内容,经过运算得到一串hash值 2.hash值的特点是:2.1 只要传入的内容一样,得到的hash值必然一样=====& ...

  2. 4月2日 python学习总结

    昨天内容回顾: 1.迭代器 可迭代对象: 只要内置有__iter__方法的都是可迭代的对象 既有__iter__,又有__next__方法 调用__iter__方法==>得到内置的迭代器对象 调 ...

  3. 4月12日 python学习总结 继承和派生

    一.继承 什么是继承:   继承是一种新建类的方式,在python中支持一个子类继承多个父类   新建类称为子类或派生类   父类可以称之为基类或者超类   子类会遗传父类的属性 2.  为什么继承 ...

  4. 4月11日 python学习总结 对象与类

    1.类的定义 #类的定义 class 类名: 属性='xxx' def __init__(self): self.name='enon' self.age=18 def other_func: pas ...

  5. 4月8日 python学习总结 模块与包

    一.包 #官网解释 Packages are a way of structuring Python's module namespace by using "dotted module n ...

  6. 5月9日 python学习总结 外键、表之间的关联关系、修改表、清空表内容、复制表

    一.外键foreign key    外键约束: 1.必须先创建被关联表才能创建关联表 2.插入记录时,必须先插入被关联表的记录,才能插入关联表(要用到被关联表)的记录 3.若不设置同步更新和同步删除 ...

  7. 5月25日 python学习总结 HTML标签

    一.HTML简介  http://www.cnblogs.com/linhaifeng/articles/8973878.html 二.HTML标签与文档结构 http://www.cnblogs.c ...

  8. 6月12日 python学习总结 框架

    1. 登录功能的实现 1. form表单提交数据的注意事项: 1. 是form不是from,必须要有method和action 2. 所有获取用户输入的表单标签要放在form表单里面,表单标签必须要有 ...

  9. 5月8日 python学习总结 mysql 建表操作

    一 .创建表的完整语法 create table 表名( 字段名1 类型[(宽度) 约束条件],字段名2 类型[(宽度) 约束条件],字段名3 类型[(宽度) 约束条件]); 解释: 类型:使用限制字 ...

随机推荐

  1. 深度学习:多层感知机和异或问题(Pytorch实现)

    感知机模型 假设输入空间\(\mathcal{X}\subseteq \textbf{R}^n\),输出空间是\(\mathcal{Y}=\{-1,+1\}\).输入\(\textbf{x}\in \ ...

  2. 04.python语法入门--基本数据类型

    # python是一门解释型的.强类型的.动态语言​​# 一:数字类型# 1.1 整型int:记录人的年龄.等级.号码.个数# age = 18# print(type(age))​# 1.2 浮点数 ...

  3. GIL解释器锁 & 进程池与线程池

    今日内容 GIL 全局解释器锁(重要理论) 验证 GIL 的存在及功能 验证 python 多线程是否有用 死锁现象 进程池与线程池(使用频率高) IO模型 详细参考: https://www.bil ...

  4. 『无为则无心』Python面向对象 — 47、Python中的self详解

    目录 1.self的作用 2.self的使用注意事项 (1)self代表类的实例,而非类 (2)self不必非写成self,只是一种规范. (3)类中方法的形参中一定要写self,包括内置函数 (4) ...

  5. 攻防世界--神奇的Modbus

    使用Wireshark在数据包中寻找flag flag是明文形式存储wireshark输入modbus过滤并追踪tcp流追踪tcp流 得到结果:ctf{Easy_Modbus} (虽然在wiresha ...

  6. AWVS漏洞扫描教程之扫描方式

    实验目的 掌握AWVS的基本用法. 实验原理 Acunetix Web Vulnerability Scanner是一个网站及服务器漏洞扫描软件. 实验内容 对Web站点进行安全测试. 实验环境描述 ...

  7. RFC3918组播组容量测试——网络测试仪实操

    一.简介 1.RFC3918简介 历史 · 在1999年3月成为正式标准 功能 · 评测网络互连设备或网络系统的性能 · 网络设备: 交换机,路由器- 内容 · 定义了一整套测试方法,为不同厂家的设备 ...

  8. 【C# 线程】线程局部存储(TLS) 实战部分 ThreadStatic|LocalDataStoreSlot|ThreadLocal<T>

    往袋子里面装苹果 错误案例示范 关于C#多线程的文章,大部分都在讨论线程的起停或者是多线程同步问题.多线程同步就是在不同线程中访问同一个变量(一般是线程工作函数外部的变量),众所周知在不使用线程同步的 ...

  9. 每日一算之变位词(C#)

    今天看编程珠玑里面,看到一个关于查找变位词的题目,大概意思如下:post,stop,tops这几个是变位词,找出类似的这些词语来. 解题思路一:既然是变位词,1.他们的长度一定是一致的:2.还有就是他 ...

  10. Windows端口开启关闭

    转至:https://www.cnblogs.com/shenyiyangle/p/10503754.html netstat-a #显示所有活动的TCP连接以及计算机监听的TCP和UDP端口. ne ...