mysql中运用条件判断筛选来获取数据
### part1 单表查询
sql查询完整语法:
select .. from .. where .. group by .. having .. order by .. limit ..
一.where 条件的使用
"""功能:对表中的数据进行过滤筛选"""
"""
语法:
	1.判断条件的符号
	= > < >= <= != <>(不等于)
	2.拼接条件关键字
	and or not
	3.查询区间范围值
	between 小值 and 大值 [小值,大值] 查找两者之间的范围
	4.查找某个具体范围值
	in(值1,值2,值3) 在括号里这个范围内查询
	5.模糊查询 like '%' 通配符
		like '%a'  匹配以a结尾的任意长度的字符串
		like 'a%'  匹配以a开头的任意长度的字符串
		like '%a%' 匹配字符串中含有a字符的字符串
		like '_a'  一共是2个长度,以a结尾,前面那个字符是什么无所谓
		like 'a__' 一共是3个长度,以a开头,后面是什么字符无所谓
"""
# (1) 单条件查询:
# 查询部门是sale的所有员工姓名:
select emp_name from  employee where post = 'sale';
# (2) 多条件查询
# 部门是teacher,并且收入大于10000的所有数据
select * from employee where post = "teacher" and salary > 10000 ;
# (3) 关键 between .. and
# 收入在1万到2万之间的所有姓名和收入
select emp_name,salary from employee where salary between 10000 and 20000;
# 收入不在1万到2万之间的所有姓名和收入
select emp_name,salary from employee where salary not between 10000 and 20000;
# (4) 关键字is null (判断某个字段是不是null , 不能用等号, 只能用is)
# 查询 post_comment 是空的 null
select emp_name,salary,post_comment from employee where post_comment is null;
# 表达 post_comment 不是空的 is not null
select emp_name,salary,post_comment from employee where post_comment is not null;
select emp_name,salary,post_comment from employee where post_comment = null;
# 设置一个值是空的
update employee set post_comment = '' where id = 3;
select emp_name,salary,post_comment from employee where post_comment = '';
# (5) 关键字in的查询
# 查收入是 3000 或者 2500 或者 4000 或者9000的所有员工和收入.
select emp_name,salary from employee where salary=3000 or salary=2500 or salary=4000 or salary=9000;
# 优化: 在当前括号里面查找
select emp_name,salary from employee where salary in(3000,2500,4000,9000);
# 在这个范围用in ,不在这个范围用not in
select emp_name,salary from employee where salary not in(3000,2500,4000,9000,3000.13);
# (6) 关键字like 模糊查询
# 1. 通配符 %
select * from employee where emp_name like "%on";
# 2. 通配符 _
select * from employee where emp_name like "a_e_";
# (7) concat sql内置函数 concat(参数1,参数2,参数3) 把所有参数拼接在一起
# as 用来起别名
select emp_name,concat("姓名:",emp_name,"薪水:",salary) as ss  from employee;
# concat_ws("符号",参数1,参数2,参数3) 第一个参数是分隔符,后面写上要拼接的参数
select emp_name,concat_ws(" : ",emp_name,salary) as aa from employee;
# 在sql中可以做四则运算(+ - * /)
select emp_name,concat_ws(" : ",emp_name,salary*12) as aa from employee;
二.group by 分组:
"""group by 分组分类 by 后面的字段 一般是select 后面要搜索的字段"""
select post from employee where depart_id > 1 group by post
# group_concat 按照分组的形式拼接字段
select group_concat(emp_name),post from employee where depart_id > 1 group by post;
# 聚合函数:
	# 统计总数   count *代表所有.
	select count(*) from employee;
	# 统计最大值 max
	select max(salary) from employee;
	# 统计最小值 min
	select min(salary) from employee;
	# 统计平均值 avg
	select avg(salary) from employee;
	# 统计总和sum
	select sum(salary) from employee;
# 一般来说 分组 + 聚合函数在一起使用
	# 求各部门的平均工资
	select post,avg(salary) from employee group by post
	select depart_id,avg(salary) from employee group by depart_id
	# 查询部门名以及各部门的最高薪资
	select post,max(salary) from employee group by  post
	# 查询公司内男员工和女员工的个数
	select sex,count(*)  from employee group by sex
	# 查询部门名以及部门包含的所有员工名字
	select post,emp_name from employee group by post,emp_name
	select group_concat(emp_name),post from employee group by post;
三.having 查询数据之后在进行过滤 , 一般是配合group by 使用,主要用于分组之后在过滤
# 比如:求各个部门平均薪资,找出平均薪资大于10000以上的所有部门
select post,avg(salary) from employee group by post having avg(salary) > 10000;
# 1.查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数
select post,count(*),group_concat(emp_name) from employee group by post having count(*) < 2
# 2.查询各岗位平均薪资小于10000的岗位名、平均工资
select post,avg(salary) from employee group by post having avg(salary) < 10000;
# 3.查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资
(1)select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary) < 20000
(2)select post,avg(salary) from employee group by post having avg(salary) between 10000 and 20000;
四.order by 按照什么字段排序
# 默认升序asc 从小到大排序
select  emp_name,age,post from employee order by age
select  emp_name,age,post from employee order by age asc
# 倒序 desc 从大到小排序
select emp_name from employee where post ="teacher" order by age desc
# 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;
# 3. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资降序排列
select post,avg(salary) from employee group by post having avg(salary) > 10000 order by avg(salary) desc;
五.limit 限制查询的条数[用于做数据分页]
# limit m,n 默认m值是0 代表第一条数据,n所代表的是查询几条,从m+1条件开始,查询n条数据
select * from employee limit 0,5
# 从第6条,继续往下搜,搜5条数据.
select * from employee limit 5,5
# 从第11条,继续往下搜,搜5条数据.
select * from employee limit 10,5
# 查询最后一条数据 limit 一个参数,就是查询几条的意思.
select * from employee order by id desc limit 1
select * from employee order by id desc limit 3
六.使用正则表达式查询数据(了解,不好用,查询速度慢,部分结果与python 不一致)
select * from employee where emp_name regexp 'on$';
select * from employee where emp_name regexp '^程';
select * from employee where emp_name regexp '程.*金'; # .*? 这里的?号识别不了
### part2 多表查询
# 内连接(内联查询 inner join ) : 两表或者多表满足条件的数据查询出来 [表与表之间都有的部分会查出来]
"""
语法:
	双表的内联:select 字段 from 表1 inner join  表2 on 条件
	多表的内联:select 字段 from 表1 inner join  表2 on 条件 inner join 表3 on 条件 ... ...
"""
# 基本写法:
select * from employee inner join department on employee.dep_id =  department.id
# 用as 起别名
select * from employee as e inner join department as d on e.dep_id =  d.id
# as 也可以省略
select * from employee e inner join department  d on e.dep_id =  d.id
# 用普通的where 条件来进行查询 默认使用的内联方式
select * from employee,department where employee.dep_id = department.id
select * from employee as e,department as d where e.dep_id = d.id
# 外连接
	#(1)左连接(左联查询 left join): 以左表为主,右表为辅,完整查询左表数据,右表没有的数据补null
	"""select 字段 from 表1 left join 表2 on 条件"""
	select * from employee left join department on employee.dep_id =  department.id
	#(2)右链接(右联查询 right join):以右表为主,左表为辅,完整查询右表数据,左表没有的数据补null
	"""select 字段 from 表1 right join 表2 on 条件"""
	select * from employee right join department on employee.dep_id =  department.id
	#(3)全连接(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
	# 例子1:找出年龄大于25岁的员工姓名及所在部门名字
	# 内联:
	select
		employee.id , employee.name as en , department.name as dn
	from
		employee inner join department on employee.dep_id = department.id
	where
		age > 25
	# 例子2:查询employee 和 department 关联数据,以age字段升序排序
	# 内联
	select
			*
	from
		employee inner join department on employee.dep_id = department.id
	order by
		age desc
	# where写法:
	select *
	from
		employee,department
	where
		 employee.dep_id = department.id
	order by
		age desc
### part3 子查询
"""
子查询:嵌套查询
	1.子查询是讲一个查询语句嵌套在另外一个查询语句之中,用括号()抱起来,表达一个整体
	2.一般应用在from 或者 where 或者 字段中 .子查询这个整体可以作为表,也可以作为where 后面条件表达式
	3. 速度从快到慢: 单表查询速度最快 >  其他是联表操作 > 子查询
"""
# (1)找出平均年龄大于25岁以上的部门
# 1.普通的where 联表查询
select
	d.name ,d.id
from
	employee e ,department  d
where
	e.dep_id = d.id
group by
	d.id,d.name
having
	avg(e.age) > 25
# 2.内联查询
select
	d.id,d.name
from
	employee e inner join department d on e.dep_id = d.id
group by
	d.id,d.name
having
	avg(e.age) > 25
# 3.子查询
# 1.先选出平均年龄大于25岁的部门id
select dep_id from employee group by dep_id  having avg(age) > 25;
# 2.根据结果,选出在这个范围中的数据
select name from department where id in(201,202)
# 综合拼接:
select
	name
from
	department
where
	id in(select dep_id from employee group by dep_id  having avg(age) > 25)
# (2)查看技术部门员工姓名
# 1.普通的where 联表查询
select
	 e.name
from
	employee e ,department d
where
	e.dep_id = d.id and d.name = "技术"
# 2.内联查询
select
	e.name
from
	employee e inner join department d on e.dep_id = d.id
where
	d.name = "技术";
# 3.子查询
# 1.找技术部门对应的id是谁
select id from department where name = "技术"
# 2.通过id找员工姓名
select name from employee dep_id = ?
# 3.综合拼接
select name from employee where  dep_id = (select id from department where name = "技术")
# (3)查看哪个部门没员工
# 右联方法:
select
	 d.name
from
	employee e right join department d on e.dep_id = d.id
where
	e.dep_id is null
# 子查询:
# (1) 先查询 员工都在哪些部门
select dep_id from employee group by dep_id
# (2) 把不在部门列表中的这个部门找出来
select id from department where id not in ?
# 综合拼接
select id from department where id not in(select dep_id from employee group by dep_id)
# (4)查询大于平均年龄的员工名与年龄
# 假如平均年龄是28岁
select * from 表 where age > 28
# 找平均年龄
select avg(age) from employee
# 综合拼接
select name,age from employee where age > (select avg(age) from employee)
# (5)把大于其本部门平均年龄的员工名和姓名查出来
select * from employee
+----+------------+--------+------+--------+
| id | name       | sex    | age  | dep_id | avg(age)
+----+------------+--------+------+--------+
|  1 | egon       | male   |   18 |    200 | 10
|  2 | alex       | female |   48 |    201 | 11
|  3 | wupeiqi    | male   |   38 |    201 | 11
|  4 | yuanhao    | female |   28 |    202 | 13
|  5 | liwenzhou  | male   |   18 |    200 | 10
|  6 | jingliyang | female |   18 |    204 | 15
+----+------------+--------+------+--------+
#(1) 先计算各个部门平均年龄
select dep_id,avg(age) from employee group by dep_id;
#(2) 让子查询单独作为一张临时表和employee联表变成一张更大的表
select *
from
	employee t1 inner join
	(select dep_id,avg(age) age from employee group by dep_id) as t2
	on t1.dep_id = t2.dep_id
#(3) 让这张大表,当成一次单表查询;
select *
from
	employee t1 inner join
	(select dep_id,avg(age) as age from employee group by dep_id) as t2
	on t1.dep_id = t2.dep_id
where
	t1.age > t2.age
# (6)查询每个部门最新入职的那位员工  # 利用上一套数据表进行查询;
"""
# 每个部门都对应很员工,每个员工都对应一个入职时间,如果这时间最大就代表放入职
"""
# 找每个部门的最大入职时间
	select post,max(hire_date) from employee group by post;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | emp_name   | sex    | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |  max时间
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
|  1 | egon       | male   |  18 | 2017-03-01 | 老男孩驻沙河办事处外交大使              | NULL         |    7300.33 |    401 |         1 | 1
|  2 | alex       | male   |  78 | 2015-03-02 | teacher                                 | NULL         | 1000000.31 |    401 |         1 |  2
|  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher                                 |              |    8300.00 |    401 |         1 |  3
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    401 |         1 |  4
|  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 |  5
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         |    9000.00 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.00 |    401 |         1 |
|  8 | 成龙       | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    401 |         1 |
|  9 | 歪歪       | female |  48 | 2015-03-11 | sale                                    | NULL         |    3000.13 |    402 |         2 |
| 10 | 丫丫       | female |  38 | 2010-11-01 | sale                                    | NULL         |    2000.35 |    402 |         2 |
| 11 | 丁丁       | female |  18 | 2011-03-12 | sale                                    | NULL         |    1000.37 |    402 |         2 |
| 12 | 星星       | female |  18 | 2016-05-13 | sale                                    | NULL         |    3000.29 |    402 |         2 |
| 13 | 格格       | female |  28 | 2017-01-27 | sale                                    | NULL         |    4000.33 |    402 |         2 |
| 14 | 张野       | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 |
| 15 | 程咬金     | male   |  18 | 1997-03-12 | operation                               | NULL         |   20000.00 |    403 |         3 |
| 16 | 程咬银     | female |  18 | 2013-03-11 | operation                               | NULL         |   19000.00 |    403 |         3 |
| 17 | 程咬铜     | male   |  18 | 2015-04-11 | operation                               | NULL         |   18000.00 |    403 |         3 |
| 18 | 程咬铁     | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
"""
先把需要连接的字段查出来(最大值),然后在和旧表连接成想要的新表数据,从这个新的大表当中,搜索想要的字段.
"""
select
*
from
employee as t1 inner join
(select post,max(hire_date) as max_date from employee group by post) as t2
on t1.post = t2.post
where
t1.hire_date = t2.max_date
# (7)带EXISTS关键字的子查询
"""
exists 关键字表示存在
	如果内层sql能够查到数据,返回真True,外层sql执行查询操作
	如果内层sql不能查到数据,返回False ,外层sql 就不执行查询操作;
"""
select * from employee
where exists
(select * from department where id = 200)
总结:
"""
子查询可以单独作为一个子句,也可以作为一个表或者某个字段
一般用在where from select 后面的一个字段
通过查询出来的临时表,可以在和其他表数据进行联接,形成一张更大的表.
然后可以把更大的表当成一次单表查询操作,拿出想要的字段和条件完成任务;
"""mysql中运用条件判断筛选来获取数据的更多相关文章
- Mysql中的条件语句if、case
		Mysql中的条件语句在我们对数据进行转换的时候比较有用,这样就不需要创建中转表. IF 函数 IF(expr1,expr2,expr3) 如果 expr1 是TRUE (expr1 <> ... 
- Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件
		一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ... 
- sql 语句中使用条件判断case then else end
		sql 语句中使用条件判断case then else end范例: SELECT les.[nLessonNo] FROM BS_Lesson AS les WHERE les.[sClassCod ... 
- shell中的条件判断以及与python中的对比
		shell中比如比较字符串.判断文件是否存在及是否可读等,通常用"[]"来表示条件测试. 注意:这里的空格很重要.要确保方括号的空格. if ....; then ... 
- Shell编程中的条件判断(条件测试)
		Shell中的条件判断(测试)类型: 1) 整数测试 2) 字符测试 3) 文件测试 条件测试的表达式: (注: expression 与 [] 之间空格不能省略) [ expressi ... 
- mysql中的if判断
		问题是这样的,有一张表(tb_class)专门保存班级的ID和班级的名字 另一张表是学生信息表(tb_stu),表中有一个字段叫classID,没有外键关联,现在要把 这张表刷新到另一个表tb_par ... 
- 完全总结bash中的条件判断test [ [[ 使用
		在bash脚本编程中,我们经常做一些条件判断, 我们主要用到了三种,test,单中括号,双中括号 经常有看到不同的写法,如: [ $? –eq ] [[ $myvar == “mysql” ]] te ... 
- MySQL的简单条件判断语句
		在MySQL中条件判断语句常用于数据转换,基于现有数据创建新的数据列,使用场景还是比较多. 基础样式: CASE WHEN`条件`THEN`结果` ELSE`默认结果` END 在同一条判断语句中可以 ... 
- shell中的条件判断if和测试
		(一)条件判断 if 中-z 到 -d 的意思 [ -a file ] 若file存在,则为真. [ -b file ] 若file存在且是一个块特殊文件,则为真. [ -c file ] 若file ... 
随机推荐
- SteamVR Plugin
			使用HTC vive基于unity做虚拟现实,需要用到steamVR插件,最近查找了很多资料,稍微做一下总结. 做虚拟现实无非是头显在场景中的camera功能以及手柄的操作功能. (一)camera以 ... 
- Application,Session,Cookie,ViewState,Cache对象用法、作用域的区别
			1.Application:用于保存所有用户共用的数据信息.在Asp.Net中类似的配置数据最好保存在Web.config文件中.如果使用Application对象,一个需要考虑的问题是任何写操作都要 ... 
- 简要概括java技术体系
			以前一直在学java程序设计语言,学完了就以为自己已经把java学得差不多了,直到最近在看一本书<深入理解java虚拟机>,才发现自己以前学的只不过是冰山一角.相信很多小伙伴跟我一样,在没 ... 
- Java匹马行天下之一顿操作猛如虎,框架作用知多少?
			流行框架: 框架就是开发人员定义好的一套模板,程序员只需要往模板中添加响应的代码即可,填完代码,项目就完成了.所以框架存在的意义以及我们学习框架的目的就是想办法能够让程序员快速的完成整个项目的开发.理 ... 
- Mybatis:CRUD操作
			提示: Mapper配置文件的命名空间为对应接口包名+接口名字,这个经常会忘记和搞错的!! select标签 在接口中编写三个查询方法 //获取全部用户List<User> selectU ... 
- python中“end=”用法
			python中“end=”用法:例如print(“#”,end=" \n"),默认换行,print(“#”,end=" ")则在循环中不换行 
- 游戏辅助外gua篇:如何Dump内存获得游戏的辅助
			转载请标明出处: https://dujinyang.blog.csdn.net/article/category/9267855 本文出自:[奥特曼超人的博客] 本篇邀请了 "阿七&quo ... 
- Python 基础 三 反射
			Python 基础 三 反射 今天我们先介绍一下反射这个概念,啥是反射?反射就是自己检测自己.在我们Python的面向对象中的反射是啥意思呢?就是通过字符串的形式操作对象相关的属性.python中的一 ... 
- 20190806-sed面试题
			由于比较难,附上PPT,没事还得看 下载:https://www.lanzous.com/i5cs9aj 密码:arka 1.删除centos7系统/etc/grub2.cfg⽂件中所有以空⽩开头的⾏ ... 
- IDEA快捷键汇总
			[常用] Ctrl+Shift + Enter,语句完成 "!",否定完成,输入表达式时按 "!"键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更 ... 
