# ### part1 单表查询
# sql 查询语句的完整语法
''' select .. from .. where .. group by .. having .. order by .. limit .. '''

# 一.where 条件的使用
"""功能:对表中的数据进行筛选过滤"""

"""
语法:
1.判断的符号:
= > < >= <= != <> 不等于
2.拼接条件的关键字
and or not
3.查询的区间范围值 between
between 小值 and 大值 [小值,大值] 查询两者之间这个范围的所有数据
4.查询具体某个值的范围 in
in(1,-9,-10,"a") 指定范围
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) null关键字 在搜索的时候,要用is进行判定,不能用=
# 查询 post_comment 是空的NULL 所有数据
select * from employee where post_comment = NULL 数据是空,搜索不到
select * from employee where post_comment is NULL
select * from employee where post_comment is not NULL

update employee set post_comment = "" where id = 1
select * from employee where post_comment = '';

# (5) 关键字 in 的查询
# 查询收入是 3000 或 5000 或者 4000 或者 8000 所有员工姓名和收入
select emp_name,salary from employee where salary=3500 or salary=5000 or salary=8300 or salary=4000;
# 用in优化,在小括号里面写上可能的值
select emp_name,salary from employee where salary in (3500,5000,8300,4000);
# 不在括号中的值,搜索出来
select emp_name,salary from employee where salary not in (3500,5000,8300,4000);

# (6) 关键字 like 模糊查询
# (1) % 通配符
select emp_name,age,post from employee where emp_name like "%on";
# (2) _ 通配符
select emp_name,age,post from employee where emp_name like "a_e_";

# (7) concat
select concat("姓名:",emp_name,"薪资:",salary) as aaa from employee;
# concat_ws(拼接的符号,参数1,参数2,参数3 ... )
select concat_ws(" : ",emp_name,salary) as bbb from employee;
# 可以在sql中使用四则运算(+ - * /)
select concat_ws(" : ",emp_name, salary * 12 ) as bbb from employee;

# 二.group by 子句 分组,分类
"""group by 对数据进行分类, by 后面接的字段,就是select要搜索的字段"""
select sex from employee group by sex;
select post from employee group by post;
# group_concat 按照分组形式进行字段的拼接
select group_concat(emp_name),post from employee where 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

# 一般来说 使用时 分组 + 聚合函数 配合使用
# 1. 查询部门名以及各部门的平均薪资
select post , avg(salary) from employee group by post;
# 2. 查询部门名以及各部门的最高薪资
select post , max(salary) from employee group by post;
# 3. 查询部门名以及各部门的最低薪资
select post , min(salary) from employee group by post;
# 4. 查询公司内男员工和女员工的个数
select sex,count(*) from employee group by sex
# 5. 查询部门名以及部门包含的所有员工名字
select group_concat(emp_name) , post from employee group by post
select emp_name,post from employee group by post,emp_name

# 三.having 查询数据之后在进行过滤,一般是配合group by使用, 主要用分组后过滤
# 找出各部门的平均薪资,并且大于10000以上的所有部门
select post,avg(salary) from employee group by post having avg(salary) > 10000;
# 1.查询各岗位内包含的员工个数小于2的岗位名,员工名,个数
select post,group_concat(emp_name),count(*) 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的岗位名、平均工资
select post,avg(salary) from employee group by post having avg(salary) between 10000 and 20000
select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary) < 20000

# 四.order by 排序 , 按照什么字段进行排序
# 默认值asc 升序排序
# 按照desc 降序排序
select * from employee order by age (默认升序)
select * from employee order by age desc (降序)

# 1. 查询所有员工信息,先按照age升序排序,如果age相同则按照hire_date降序排序
select emp_name,sex,age,hire_date,post from employee order by age,hire_date desc
# 2. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资降序排列
select post,avg(salary) from employee group by post having avg(salary) > 10000 order by avg(salary) desc
# 3. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资升序排列
select post,avg(salary) from employee group by post having avg(salary) > 10000 order by avg(salary) asc

# 五.limit 限制查询的条数 (数据分页)
limit m,n m代表从第几条开始查询,n代表查询几条 m=0 代表的是第一条
select * from employee limit 0,5 从第一条开始查,查5条
select * from employee limit 5,5 从第六条开始查,查5条
# 只查询一条数据
select * from employee limit 1
# 想要瞬间得到数据表中,最后一条数据
select * from employee order by id desc limit 1
# 拿到最后三条数据
select * from employee order by id desc limit 3

# 六.(了解) 可以使用正则表达式查询数据 (不推荐使用,不好用效率不高)
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 条件

"""

# 基本语法 inner join 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

# ### part3 子查询
"""
子查询: 嵌套查询
(1) 子查询是查询的语句当中又嵌套的另外一条sql语句,用括号()抱起来,表达一个整体
(2) 一般应用在from 子句后面表达一张表,或者 where 子句后面表达一个条件
(3) 速度从快到慢 单表查询速度最快 -> 联表查询 -> 子查询
"""

# (1)找出平均年龄大于25岁以上的部门
# 普通的where 相当于内联查询
select
d.id,d.name
from
employee e,department d
where
e.dep_id = d.id
group by
d.id,d.name
having
avg(e.age) > 25;

# (2) inner join
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.通过部门id,找部门名字
select name from department where id in (201,202)
# 3.综合拼接:
select id,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.inner join 实现
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 where employee.dep_id = ?

# 3.综合拼接
select name from employee where employee.dep_id = (select id from department where name = "技术")

# (3)查看哪个部门没员工

# 联表写法
select
d.id,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 => (200,201,202,204)
# 2.把不在部门列表中的数据找出来
select from department where id not in (1)
# 3.综合拼接
select id,name from department where id not in (select dep_id from employee group by dep_id)

# (4)查询大于平均年龄的员工名与年龄
# 假设平均年龄是18岁
select name,age from employee where age > ?
# 找平均年龄
select avg(age) from employee
# 综合拼装
select name,age from employee where age > (select avg(age) from employee)

# (5)把大于其本部门平均年龄的员工名和姓名查出来
# employee
+----+------------+--------+------+--------+
| id | name | sex | age | dep_id || dep_id | avg(age) |
+----+------------+--------+------+--------+
| 1 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 201 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | liwenzhou | male | 18 | 200 |
| 6 | jingliyang | female | 18 | 204 |
+----+------------+--------+------+--------+
# department
+------+--------------+
| id | name |
+------+--------------+
| 200 | 技术 |
| 201 | 人力资源 |
| 202 | 销售 |
| 203 | 运营 |
+------+--------------+
# 1.先计算平均年龄
select dep_id,avg(age) from employee group by dep_id
+--------+----------+
| dep_id | avg(age) |
+--------+----------+
| 200 | 18.0000 |
| 201 | 43.0000 |
| 202 | 28.0000 |
| 204 | 18.0000 |
+--------+----------+
# 2.把子查询查出来的数据和employee作拼接,联合成一张更大的表,做一次单表查询;
select
*
from
employee as t1 inner join (1) as t2 on t1.dep_id = t2.dep_id

# 3.综合拼接
select
*
from
employee as t1 inner join (select dep_id,avg(age) as avg_age from employee group by dep_id) as t2 on t1.dep_id = t2.dep_id

# 4.把额外的比较的条件加进去
select
*
from
employee as t1 inner join (select dep_id,avg(age) as avg_age from employee group by dep_id) as t2 on t1.dep_id = t2.dep_id
where
t1.age > t2.avg_age

# (6)查询每个部门最新入职的那位员工 # 利用上一套数据表进行查询;
# 1.找每个部门最大的入职时间
select post,max(hire_date) as max_date from employee group by post

# 2.把子查询查出来的数据和employee联合成一张更大的表,做一次单表查询
select
from
employee as t1 inner join (1) as t2 on t1.post = t2.post
where
t1.hire_date = t2.max_date

# 3.综合拼接
select
t1.emp_name,t1.hire_date
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 employee where id = 1)

"""
子查询总结:
子查询可以单独作为一个子句,也可以作为一个表或者某个字段
一般用在from where select 子句后面
通过查询出来的临时表,可以跟任意的表重新拼接,组成更大的表,在通过筛选达成自己的目的
"""

mysql操作进阶的更多相关文章

  1. mysql 开发进阶篇系列 55 权限与安全(安全事项 )

    一. 操作系统层面安全 对于数据库来说,安全很重要,本章将从操作系统和数据库两个层面对mysql的安全问题进行了解. 1. 严格控制操作系统账号和权限 在数据库服务器上要严格控制操作系统的账号和权限, ...

  2. mysql 开发进阶篇系列 46 物理备份与恢复( xtrabackup的 选项说明,增加备份用户,完全备份案例)

    一. xtrabackup 选项说明 在操作xtrabackup备份与恢复之前,先看下该工具的选项,下面记录了xtrabackup二进制文件的部分命令行选项,后期把常用的选项在补上.点击查看xtrab ...

  3. mysql 开发进阶篇系列 42 逻辑备份与恢复(mysqldump 的完全恢复)

    一.概述 在作何数据库里,备份与恢复都是非常重要的.好的备份方法和备份策略将会使得数据库中的数据更加高效和安全.对于DBA来说,进行备份或恢复操作时要考虑的因素大概有如下: (1) 确定要备份的表的存 ...

  4. mysql 开发进阶篇系列 20 MySQL Server(innodb_lock_wait_timeout,innodb_support_xa,innodb _log_*)

    1. innodb_lock_wait_timeout mysql 可以自动监测行锁导致的死锁并进行相应的处理,但是对于表锁导致的死锁不能自动监测,所以该参数主要用于,出现类似情况的时候等待指定的时间 ...

  5. MySQL第二讲 一一一一 MySQL语句进阶

    通过命令来备份数据库: 通过数据库软件里面的,mysqldump模块来操作,如下: mysqldump -u root db1 > db1.sql -p; //没有-d就是备份的时候:数据表结构 ...

  6. MYSQL(进阶篇)——一篇文章带你深入掌握MYSQL

    MYSQL(进阶篇)--一篇文章带你深入掌握MYSQL 我们在上篇文章中已经学习了MYSQL的基本语法和概念 在这篇文章中我们将讲解底层结构和一些新的语法帮助你更好的运用MYSQL 温馨提醒:该文章大 ...

  7. Mysql操作初级

    Mysql操作初级 本节内容 数据库概述 数据库安装 数据库操作 数据表操作 表内容操作 1.数据库概述 数据库管理系统叫做DBMS 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建 ...

  8. python学习道路(day12note)(mysql操作,python链接mysql,redis)

    1,针对mysql操作 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); 设置密码 update user set password ...

  9. 学习笔记:MySQL操作初步

    对数据库的操作:SQL语言 一:SQL:Structured Query Language,结构化查询语言! 二:DDL:Data Definition Language,数据定义语言 三:DML:D ...

随机推荐

  1. 掘金转载-手写一个Promise

    目录 一 什么是Promise ? 二 Promises/A+ 规范 2.1 术语 2.2 基本要求 2.2.1. Promise的状态 2.2.2. Then 方法 2.3 简易版实践 2.4 进一 ...

  2. mybatis错题

    第一题 解析: MyBatis的动态SQL中没有else元素,when元素的test属性中直接书写表达式即可,即test=”表达式”. 第二题 解析: resource属性和url属性是必须的属性,但 ...

  3. (全国多校重现赛一)D Dying light

    LsF is visiting a local amusement park with his friends, and a mirror room successfully attracts his ...

  4. 新版FPC摄像头测评 OV7725 OV7670 OV9650 OV9655 OV5640 OV5642 OV2640 OV3640 MT9D112

    新版FPC摄像头测评 OV7725 OV7670 OV9650 OV9655 OV5640 OV5642 OV2640 OV3640 MT9D112 最新制样新版FPC摄像头板卡,先看看结构尺寸 再瞧 ...

  5. 【hibernate】应用程序级别的视图

    [hibernate]应用程序级别的视图 转载:https://www.cnblogs.com/yangchongxing/p/10361281.html 在没有数据库修改权限时,像创建视图可以使用 ...

  6. struct socket结构体详解

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://weiguozhihui.blog.51cto.com/3060615/15852 ...

  7. Redis有哪几种数据类型

    Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) string 是 redi ...

  8. JavaSE-知识点总结

    Java名词 变量.运算符.类.接口.枚举.参数.注解.异常.包装类.多线程.集合.IO流.网络编程.反射.Lambda.API 源文件:.java文件,存储Java源代码的文件 字节码文件:.cla ...

  9. Homebrew的安装

    Homebrew是一款Mac OS平台下的软件包管理工具. 安装方法:命令行输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubuserc ...

  10. Hadoop_HDFS_02

    1. HDFS入门 1.1 HDFS基本概念 HDFS是Hadoop Distribute File System的简称, 意为: Hadoop分布式文件系统. 是Hadoop三大核心组件之一, 作为 ...