1.1 as关键字

用于 给显示结果中字段 或者 表 起别名

select 别名.字段名 from 表名 as 别名 where 条件语句

# 对字段起别名
select id as '编号', name as '大名',age as '年龄' from students;
# 修改的显示结果中的字段名称不影响表中实际名称;别名只在当前SQL中有效 # 对表名起别名 如果需要通过表名获取字段 别名.字段
mysql> select s.id,s.name from students as s;
# select students.id,students.name from students;

as 在 MySQL中可以省略

select id '编号', name '大名',age '年龄' from students;

1.2 distinct

把显示的结果中去除重复的行数据

select distinct 字段 from 表名

mysql> select age,height,gender from students;
+------+--------+--------+
| age | height | gender |
+------+--------+--------+
| 19 | 172.00 | 男 |
| 88 | 173.00 | 男 |
| 100 | 160.00 | 女 |
| 100 | 165.00 | 未知 |
| 0 | 180.00 | NULL |
| 0 | NULL | NULL |
| 0 | NULL | NULL |
mysql> select distinct age,height,gender from students;
+------+--------+--------+
| age | height | gender |
+------+--------+--------+
| 19 | 172.00 | 男 |
| 88 | 173.00 | 男 |
| 100 | 160.00 | 女 |
| 100 | 165.00 | 未知 |
| 0 | 180.00 | NULL |
| 0 | NULL | NULL |

1.3 where 子句

应用场景 - 筛选记录中满足条件的数据 如果满足就予以显示。

# 1 2 3 判断 可以使用其中元素 也可以使用元素对应的位置(从1开始)
enum ('男','女','中性'); mysql> select * from students where gender = '男';
mysql> select * from students where gender = 1;
mysql> select * from students where age >=10 and age <= 50; # 模糊查询 只知道数据的一部分 查询数据 like
# % 匹配 任意个任意字符
# _ 匹配 1 个任意字符
mysql> select * from students where name like '张%';
mysql> select * from students where name like '张_';
mysql> select * from students where name like '张__'; # 范围查询
# 1.连续范围:字段 between 下限 and 上限 [下限, 上限]
mysql> select * from students where age between 10 and 50; # 2.不连续范围 字段名 in (值1, 2...)
mysql> select * from students where age = 19 or age = 29 or age = 39;
mysql> select * from students where age in (19,29,39); # 判空 IS NULL 判断非空 IS NOT NULL
# NULL是一种状态 不能使用 = 判断
-- select * from students where height = NULL;
mysql> select * from students where height IS NULL;
mysql> select * from students where height IS NOT NULL;

1.4 排序order by

应用场景-需要将数据按照一定顺序排列。如竞价 点击量 重要程度。

select * from 表名 order by 字段名 降序DESC|升序ASC(默认);

mysql> select * from students order by age ASC;
mysql> select * from students order by age; -- 以年龄 降序排序
mysql> select * from students order by age DESC; -- 需要用到多个字段排序: 如果第一个字段数据相等 就按第二个字段
order by 字段名 排序方式, 字段名2 排序方式,....; -- 先以年龄升序排序 如果有相等的 使用身高降序排序
mysql> select * from students order by age asc,height desc;

1.5 分页limit

应用在数据量大 一次性展示不完 一次给用户展示一页。

热搜榜 Top 50 order by 热点 降序 分页;

limit 数据下标=0,数量 # 数据下标默认从0开始 显示数量条信息
limit 30 == limit 0,30 -- 显示身高最高的前三名
mysql> select * from students order by height desc limit 0,3;
mysql> select * from students order by height desc limit 3; -- 显示身高最高的第四名-第6名
mysql> select * from students order by height desc limit 3,3; 起始下标 结束下标 100条
第1页 0 99
2 100 199
3 200 299
n (n-1)*100 100n-1
----------------------每页数据条数是 m 个---------——------------
n limit m*(n-1),m

1.6 聚合函数

对一组数据进行统计计算 得到一个值。组函数。

组函数默认对 字段为 NULL 的不统计 一个值 = 组函数(字段名)

组函数 默认情况下把整个表当做一个组来进行统计计算的

求和 sum()
select sum(字段名) from 表名; 计数/统计数量 count()
常用于统计表中数据行数 select count(*) from 表名; 平均数 avg()
mysql> select sum(height)/count(height) '平均身高'from students;
mysql> select avg(height) from students; 最大值 max()
mysql> select max(height) from students; 最小值 min()
mysql> select min(age) from students;

ifnull(参数1,参数2) 判断参数1是否为NULL 如果为 NULL 返回参数2;如果不为 NULL 返回参数1

1.7 分组

按照1个或者多个字段进行分类,每一类数据在同一个小组中,分别对每个小组进行统计。

组函数 结合分组使用 统计的不再是整个表 而是每个小组

-- 分组之后 结果 已经发生的结构上的变化 而不能使用之前的 SQL 看所有数据
-- select * from students group by gender; # 报错 -- group_concat() 所有成员的某个字段 合并在一行显示
mysql> select gender,group_concat(height) from students group by gender;
+--------+----------------------+
| gender | group_concat(height) |
+--------+----------------------+
| NULL | 180.00 |
| 男 | 172.00,173.00 |
| 女 | 160.00 |
| 未知 | 165.00 |
mysql> select gender,group_concat(name) from students group by gender;
+--------+-------------------------------+
| gender | group_concat(name) |
+--------+-------------------------------+
| NULL | 张三,张三丰,张三丰了 |
| 男 | Tom,司马狗剩 |
| 女 | jerry |
| 未知 | 老宋 |
mysql> select gender,group_concat(id,name) from students group by gender;
mysql> select gender,group_concat(name),group_concat(id) from students group by gender; -- 组函数 会对分组之后的每个小组分别统计
mysql> select gender,count(*) from students group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| NULL | 3 |
| 男 | 2 |
| 女 | 1 |
| 未知 | 1 | -- having 条件 是针对 分组结果进行过滤的 和 where 不能混淆
mysql> select gender,count(*) from students group by gender
-> having count(*) < 3; mysql> select gender,avg(age) from students group by gender
-> having avg(age) > 80; -- with rollup 在分组结果后新开一行 显示汇总结果
mysql> select gender,group_concat(name),count(*) from students group by gender with rollup;
+--------+-----------------------------------------------------+----------+
| gender | group_concat(name) | count(*) |
+--------+-----------------------------------------------------+----------+
| NULL | 张三,张三丰,张三丰了 | 3 |
| 男 | Tom,司马狗剩 | 2 |
| 女 | jerry | 1 |
| 未知 | 老宋 | 1 |
| NULL | 张三,张三丰,张三丰了,Tom,司马狗剩,jerry,老宋 | 7 | 汇总行

1.8 连接

如果需要将来自多个表的多个字段拼接在一个结果中显示

select * from (左表 join 右表 on 连接条件); --on条件用于过滤笛卡尔积(左表每行拼接右表每行)

# 内连接 inner join / join
# 取出左表和右表有关联的数据
mysql> select * from hero join gongfu on hero.gongfuid=gongfu.id;
+----+-----------+----------+----+--------------+
| id | name | gongfuid | id | name |
+----+-----------+----------+----+--------------+
| 2 | 李白 | 1 | 1 | 吟诗作对 |
| 1 | 妲己 | 2 | 2 | 魅惑 |
| 3 | 程咬金 | 3 | 3 | 三板斧 | # 外连接 outer join 内连接基础上添加额外数据
# 左外连接 left outer join / left join 内连接基础上添加来自左表的额外数据 右表位置 NULL 填充
mysql> select * from hero left join gongfu on hero.gongfuid=gongfu.id;
+----+-----------+----------+------+--------------+
| id | name | gongfuid | id | name |
+----+-----------+----------+------+--------------+
| 2 | 李白 | 1 | 1 | 吟诗作对 |
| 1 | 妲己 | 2 | 2 | 魅惑 |
| 3 | 程咬金 | 3 | 3 | 三板斧 |
| 4 | 公孙离 | 5 | NULL | NULL | # 右外连接 right outer join / right join内连接基础上添加来自右表的额外数据 左表位置 NULL 填充
mysql> select * from hero right join gongfu on hero.gongfuid=gongfu.id;
+------+-----------+----------+----+--------------+
| id | name | gongfuid | id | name |
+------+-----------+----------+----+--------------+
| 1 | 妲己 | 2 | 2 | 魅惑 |
| 2 | 李白 | 1 | 1 | 吟诗作对 |
| 3 | 程咬金 | 3 | 3 | 三板斧 |
| NULL | NULL | NULL | 4 | 沉默 |
-- 以下为数据来源:
-- 执行下面语句 构造数据表
create table hero(
id int unsigned primary key auto_increment,
name varchar(64) not null,
gongfuid int unsigned not null
); create table gongfu(
id int unsigned primary key auto_increment,
name varchar(64) not null
); insert into hero (name,gongfuid) values ('妲己',2),('李白',1),('程咬金',3),('公孙离',5);
insert into gongfu (name) values ('吟诗作对'),('魅惑'),('三板斧'),('沉默');

1.9 自连接

自关联 self join

当连接时 左表和右表的数据是来自于同一张表时 - 称为自连接

由于左表和右表名字一样 所以需要给表起别名。

左表 as 别名 join 右表 as 别名 on 左表字段 关联 右表字段

-- 导入 sql 文件
source /home/python/Desktop/areas.sql; -- 把areas city join areas pro on city.pid = pro.id当做一个虚表
mysql> select * from areas city join areas pro on city.pid = pro.id
where pro.title = '河南省';

1. 10子查询

概念: 主查询中嵌套的子查询

方式:将第一次查询的结果用作第二次查询的范围

种类: 是根据子查询返回的结果 类型

​ 标量 -子查询返回的结果是一行一列(一个值):主要使用聚合函数即可

​ 行 - 一行多列

​ 列 - 一列多行

​ 表 - 多行多列

-- 标量  求班级中 身高高于平均身高的学生信息
1 求出平均身高
select avg(height) from students;
2 根据平均身高 比较 身高大于平均身高的学习信息
select * from students where height > 170; 子查询语句:
select * from students where height > (select avg(height) from students); -- 行 求出 班级中身高最高并且年龄最大的同学
1 求出最高身高 最大年龄
select max(age), max(height) from students; 2 根据1步数据去学生表中查询信息
mysql> select * from students where age = 101 and height = 180;
mysql> select * from students where (age,height) = (101,180); 子查询语句:
select * from students where (age,height) = (select max(age), max(height) from students); -- 列 查询出所有使用了技能表中技能的所有英雄
1 获取所有的技能 id
select id from gongfu;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
2 据1步数据去英雄表中查询信息
select * from hero where gongfuid in (1,2,3,4); 子查询语句:
select * from hero where gongfuid in (select id from gongfu);

保留两位小数的写法round(avg(price),2)

1.11. 外键约束

作用:

​ 子表: 引用主表字段中数据的表 (外键在子表中)

​ 主表: 提供数据给别表使用的

当对子表中外键字段数据进行更新时 要求新值必须在主表对应字段中存在,如不存在则 更新失败

如果创建外键失败 1SQL错误 2表中已有数据不满足外键约束 3子表外键字段类型和主表不一样

创建外键:

表已经存在:添加外键约束
alter table 子表名 add foreign key (子表字段) references 主表名 (主表字段); 创建子表同时添加外键约束
create table haha(
字段名 类型 约束,
....,
foreign key (字段名) references 主表名(主表字段)
) 查看外键约束名称
show create table hero;
CONSTRAINT `hero_ibfk_1` FOREIGN KEY // hero_ibfk_1就是外键约束名称 用来删除外键约束 删除外键约束 知道外键约束的名字
alter table 表名 drop foreign key 外键约束名;

外键还有一种提法:一对多 和 一对一。

当一对一时:

当一对多时:多是子表,一是主表。 一般主表(一)中的一个字段能关联到子表(多)中的多个字段。

例如: 表一:游戏名 表二:游戏中的英雄名。

​ 显然,一款游戏中有多个英雄。所以表一是主表(一),表二是子表(多)。英雄表的每一个字段都由一个外键连接到游戏表中的唯一一款游戏。外键也是在游戏表中。

MySql查询进阶的更多相关文章

  1. MySQL查询(进阶)(每个标点都是重点)

    MySQL 是工作中很普遍的需要用到的,所以必须掌握,而 之前我们一直说的都是怎么存. 你只会存不会取有个屁用.所以希望大家在如何查询读取数据这方面多下点功夫. 这篇和上一篇都是干货,我也是第一次学. ...

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

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

  3. day95:flask:SQLAlchemy数据库查询进阶&关联查询

    目录 1.数据库查询-进阶 1.常用的SQLAlchemy查询过滤器 2.常用的SQLAlchemy查询结果的方法 3.filter 4.order_by 5.count 6.limit&of ...

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

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

  5. mysql查询性能优化

    mysql查询过程: 客户端发送查询请求. 服务器检查查询缓存,如果命中缓存,则返回结果,否则,继续执行. 服务器进行sql解析,预处理,再由优化器生成执行计划. Mysql调用存储引擎API执行优化 ...

  6. Mysql查询——深入学习

    1.开篇 之前上一篇的随笔基本上是单表的查询,也是mysql查询的一个基本.接下来我们要看看两个表以上的查询如何得到我们想要的结果. 在学习的过程中我们一起进步,成长.有什么写的不对的还望可以指出. ...

  7. Mysql 查询练习

    Mysql 查询练习 ---创建班级表 create table class( cid int auto_increment primary key, caption ) )engine=innodb ...

  8. mysql 查询去重 distinct

    mysql 查询去重 distinct   待完善内容..

  9. MySQl查询区分大小写的解决办法

    通过查询资料发现需要设置collate(校对) . collate规则: *_bin: 表示的是binary case sensitive collation,也就是说是区分大小写的 *_cs: ca ...

随机推荐

  1. State Threads之co-routine的创建和stack的管理

    1. 综述 协程库 State Threads Library 是一个基于 setjmp/longjmp 实现的 C 语言版用户线程库或协程库(user level thread). 基本协程例子: ...

  2. php改变header头返回值

    $code = '400 Bad Request'; header('HTTP/1.1 '.$code);

  3. SAX解析示例代码和原理

    import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; ...

  4. Sql UpdateOrInsert

    SqlServer(先更新,受影响条数为0,则Insert,通过事务): begin tran update table set column=columnvalue where wherestr b ...

  5. EL表达式与JSTL标签库(二)

    1.JSTL标签库 标签库 作用 URI 前缀 核心 包含Web应用的常见工作,如循环.输入输出等 http://java.sun.com/jsp/jstl/core c 国际化 语言区域.消息.数字 ...

  6. bazel编译im2txt的问题

    问题: 原本可以正常运行的程序,出现找不到tensorflow的问题.打印出来sys.version和sys.path,发现python版本并不是conda环境的版本 (tensorflow) yua ...

  7. FTP简单搭建(一)

    一.FTP服务介绍 vsftp(very security ftp file transfer protocol 非常文件传输协议) FTP分为主动模式和被动模式. 主动模式:(不安全,传数据的端口是 ...

  8. linux c++模拟简易网络爬虫

    /* * To change this license header, choose License Headers in Project Properties. * To change this t ...

  9. CMMI将能力成熟度分为5个级别

    CMMI将能力成熟度分为5个级别(初始级,已管理级,已定义级,量化管理级,优化级) . 初始级 此时软件过程是无序的,有时甚至是混乱的,对过程几乎没有定义,成功取决于个人努力.管理是反应式的. .可管 ...

  10. POP与OOP编程模式对比

    面向过程:(procedure oriented programming 即:POP) 代表:C/C++ 向过程程序设计,它是以功能为中心来进行思考和组织的一种编程方式,强调的是系统的数据被加工和处理 ...