### 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中运用条件判断筛选来获取数据的更多相关文章

  1. Mysql中的条件语句if、case

    Mysql中的条件语句在我们对数据进行转换的时候比较有用,这样就不需要创建中转表. IF 函数 IF(expr1,expr2,expr3) 如果 expr1 是TRUE (expr1 <> ...

  2. Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件

    一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ...

  3. sql 语句中使用条件判断case then else end

    sql 语句中使用条件判断case then else end范例: SELECT les.[nLessonNo] FROM BS_Lesson AS les WHERE les.[sClassCod ...

  4. shell中的条件判断以及与python中的对比

    shell中比如比较字符串.判断文件是否存在及是否可读等,通常用"[]"来表示条件测试. 注意:这里的空格很重要.要确保方括号的空格. if ....; then          ...

  5. Shell编程中的条件判断(条件测试)

    Shell中的条件判断(测试)类型: 1) 整数测试 2) 字符测试 3) 文件测试 条件测试的表达式:        (注: expression 与 [] 之间空格不能省略) [ expressi ...

  6. mysql中的if判断

    问题是这样的,有一张表(tb_class)专门保存班级的ID和班级的名字 另一张表是学生信息表(tb_stu),表中有一个字段叫classID,没有外键关联,现在要把 这张表刷新到另一个表tb_par ...

  7. 完全总结bash中的条件判断test [ [[ 使用

    在bash脚本编程中,我们经常做一些条件判断, 我们主要用到了三种,test,单中括号,双中括号 经常有看到不同的写法,如: [ $? –eq ] [[ $myvar == “mysql” ]] te ...

  8. MySQL的简单条件判断语句

    在MySQL中条件判断语句常用于数据转换,基于现有数据创建新的数据列,使用场景还是比较多. 基础样式: CASE WHEN`条件`THEN`结果` ELSE`默认结果` END 在同一条判断语句中可以 ...

  9. shell中的条件判断if和测试

    (一)条件判断 if 中-z 到 -d 的意思 [ -a file ] 若file存在,则为真. [ -b file ] 若file存在且是一个块特殊文件,则为真. [ -c file ] 若file ...

随机推荐

  1. fenby C语言 P25

    二维数组 #include <stdio.h> int main(){ int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}; int sum=0, ...

  2. SpringCloud之Nacos服务注册(十八)

    一 服务提供配置 pom.xml <dependency> <groupId>org.springframework.boot</groupId> <arti ...

  3. 设计模式C++描述----15.策略(Strategy)模式

    一. 举例说明 以前做了一个程序,程序的功能是评价几种加密算法时间,程序的使用操作不怎么变,变的是选用各种算法. 结构如下: Algorithm:抽象类,提供算法的公共接口. RSA_Algorith ...

  4. HDU5950 矩阵快速幂(巧妙的递推)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f[n] = 2*f[n-2] + f[n-1] + n^4 思路:对于递推题而言,如果递 ...

  5. python-nmap使用及案例

    nmap概念及功能 概念 NMap,也就是Network Mapper,最早是Linux下的网络扫描和嗅探工具包. nmap是一个网络连接端扫描软件,用来扫描网上电脑开放的网络连接端.确定哪些服务运行 ...

  6. linux(CentOS release 6.5)环境搭建svn

    正文之前,说几句关于svn和git的闲话. 之前用的版本控制工具主要都是svn,随着时间的推移,git以其强大灵活的分支管理功能受到大众喜爱.尤其是多人同时开发时同一项目,且不同部分功能时,git的分 ...

  7. [考试反思]1026csp-s模拟测试89:不公

    稍垃圾.因为T1没A. 赶巧前一段时间学了杜教筛,结果因为教练放错题. 然后考场上疯狂yy,最后水到了一个AC. 其实的确挺不公平的,不少人也没学呢. 如果只算T1和T3的分数的话,那70分就是个垃圾 ...

  8. 面试精选:JVM垃圾回收

    Tips:关注公众号:松花皮蛋的黑板报,领取程序员月薪25K+秘籍,进军BAT必备! Java堆中存放着大量的Java对象实例,在垃圾收集器回收内存前,第一件事情就是确定哪些对象是“活着的”,哪些是可 ...

  9. Salesforce学习之路-developer篇(五)Aura组件原理及常用属性

    很喜欢曾经看到的一句话:以输出倒逼输入.以输出的形式强制自己学习,确实是高效的学习方式,真的很棒.以下仅为个人学习理解,如有错误,欢迎指出,共同学习. 1. 什么是Lightning Componen ...

  10. Project Euler 55: Lychrel numbers

    五十五.吕克雷尔数(Lychrel numbers) 如果我们把\(47\)翻转过来并和其自身相加,结果是\(47+74=121\)是一个回文数.并不是所有的数都可以这么快的变成回文数,比如说: \[ ...