1.1 as关键字

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

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

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

as 在 MySQL中可以省略

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

1.2 distinct

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

select distinct 字段 from 表名

  1. mysql> select age,height,gender from students;
  2. +------+--------+--------+
  3. | age | height | gender |
  4. +------+--------+--------+
  5. | 19 | 172.00 | |
  6. | 88 | 173.00 | |
  7. | 100 | 160.00 | |
  8. | 100 | 165.00 | 未知 |
  9. | 0 | 180.00 | NULL |
  10. | 0 | NULL | NULL |
  11. | 0 | NULL | NULL |
  12. mysql> select distinct age,height,gender from students;
  13. +------+--------+--------+
  14. | age | height | gender |
  15. +------+--------+--------+
  16. | 19 | 172.00 | |
  17. | 88 | 173.00 | |
  18. | 100 | 160.00 | |
  19. | 100 | 165.00 | 未知 |
  20. | 0 | 180.00 | NULL |
  21. | 0 | NULL | NULL |

1.3 where 子句

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

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

1.4 排序order by

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

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

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

1.5 分页limit

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

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

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

1.6 聚合函数

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

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

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

  1. 求和 sum()
  2. select sum(字段名) from 表名;
  3. 计数/统计数量 count()
  4. 常用于统计表中数据行数 select count(*) from 表名;
  5. 平均数 avg()
  6. mysql> select sum(height)/count(height) '平均身高'from students;
  7. mysql> select avg(height) from students;
  8. 最大值 max()
  9. mysql> select max(height) from students;
  10. 最小值 min()
  11. mysql> select min(age) from students;

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

1.7 分组

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

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

  1. -- 分组之后 结果 已经发生的结构上的变化 而不能使用之前的 SQL 看所有数据
  2. -- select * from students group by gender; # 报错
  3. -- group_concat() 所有成员的某个字段 合并在一行显示
  4. mysql> select gender,group_concat(height) from students group by gender;
  5. +--------+----------------------+
  6. | gender | group_concat(height) |
  7. +--------+----------------------+
  8. | NULL | 180.00 |
  9. | | 172.00,173.00 |
  10. | | 160.00 |
  11. | 未知 | 165.00 |
  12. mysql> select gender,group_concat(name) from students group by gender;
  13. +--------+-------------------------------+
  14. | gender | group_concat(name) |
  15. +--------+-------------------------------+
  16. | NULL | 张三,张三丰,张三丰了 |
  17. | | Tom,司马狗剩 |
  18. | | jerry |
  19. | 未知 | 老宋 |
  20. mysql> select gender,group_concat(id,name) from students group by gender;
  21. mysql> select gender,group_concat(name),group_concat(id) from students group by gender;
  22. -- 组函数 会对分组之后的每个小组分别统计
  23. mysql> select gender,count(*) from students group by gender;
  24. +--------+----------+
  25. | gender | count(*) |
  26. +--------+----------+
  27. | NULL | 3 |
  28. | | 2 |
  29. | | 1 |
  30. | 未知 | 1 |
  31. -- having 条件 是针对 分组结果进行过滤的 where 不能混淆
  32. mysql> select gender,count(*) from students group by gender
  33. -> having count(*) < 3;
  34. mysql> select gender,avg(age) from students group by gender
  35. -> having avg(age) > 80;
  36. -- with rollup 在分组结果后新开一行 显示汇总结果
  37. mysql> select gender,group_concat(name),count(*) from students group by gender with rollup;
  38. +--------+-----------------------------------------------------+----------+
  39. | gender | group_concat(name) | count(*) |
  40. +--------+-----------------------------------------------------+----------+
  41. | NULL | 张三,张三丰,张三丰了 | 3 |
  42. | | Tom,司马狗剩 | 2 |
  43. | | jerry | 1 |
  44. | 未知 | 老宋 | 1 |
  45. | NULL | 张三,张三丰,张三丰了,Tom,司马狗剩,jerry,老宋 | 7 | 汇总行

1.8 连接

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

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

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

1.9 自连接

自关联 self join

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

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

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

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

1. 10子查询

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

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

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

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

​ 行 - 一行多列

​ 列 - 一列多行

​ 表 - 多行多列

  1. -- 标量 求班级中 身高高于平均身高的学生信息
  2. 1 求出平均身高
  3. select avg(height) from students;
  4. 2 根据平均身高 比较 身高大于平均身高的学习信息
  5. select * from students where height > 170
  6. 子查询语句:
  7. select * from students where height > (select avg(height) from students);
  8. -- 求出 班级中身高最高并且年龄最大的同学
  9. 1 求出最高身高 最大年龄
  10. select max(age), max(height) from students;
  11. 2 根据1步数据去学生表中查询信息
  12. mysql> select * from students where age = 101 and height = 180;
  13. mysql> select * from students where (age,height) = (101,180);
  14. 子查询语句:
  15. select * from students where (age,height) = (select max(age), max(height) from students);
  16. -- 查询出所有使用了技能表中技能的所有英雄
  17. 1 获取所有的技能 id
  18. select id from gongfu;
  19. +----+
  20. | id |
  21. +----+
  22. | 1 |
  23. | 2 |
  24. | 3 |
  25. | 4 |
  26. 2 1步数据去英雄表中查询信息
  27. select * from hero where gongfuid in (1,2,3,4);
  28. 子查询语句:
  29. select * from hero where gongfuid in (select id from gongfu);

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

1.11. 外键约束

作用:

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

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

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

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

创建外键:

  1. 表已经存在:添加外键约束
  2. alter table 子表名 add foreign key (子表字段) references 主表名 (主表字段);
  3. 创建子表同时添加外键约束
  4. create table haha(
  5. 字段名 类型 约束,
  6. ....,
  7. foreign key (字段名) references 主表名(主表字段)
  8. )
  9. 查看外键约束名称
  10. show create table hero;
  11. CONSTRAINT `hero_ibfk_1` FOREIGN KEY // hero_ibfk_1就是外键约束名称 用来删除外键约束
  12. 删除外键约束 知道外键约束的名字
  13. 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. SRS之SrsConfig类

    1. 类定义 1.1 SrsConfig 类 /** * the config service provider. * for the config supports reload, so never ...

  2. Ansible 快速安装配置,常用模块

    Ansible是一个轻量级的工具,基于python语言实现,通过python中的paramiko来连接并管理机器, 功能强大(YAML,PlayBook,模块化功能),不需要安装客户端, 通过ssh连 ...

  3. js调用后台接口进行下载

    js调用后台接口一定不能用ajax location.href=$$pageContextPath +'downfile/down.do?filname='+row.fileUrl;

  4. Sql语法树示例 select username, ismale from userinfo where age > 20 and level > 5 and 1 = 1

    select username, ismale from userinfo where age > 20 and level > 5 and 1 = 1 --END-2019年9月5日17 ...

  5. mybatis的mapper映射文件

    1概述1.1应用架构     mybatis框架用于支持对关系数据库的操作,该体系的应用架构如下图所示: 在mybatis框架体系中,主要的组件是:SqlSessionFactoryBean和Mapp ...

  6. 自定义有焦点的TextView实现广告信息左右一直滚动的跑马灯效果

    import android.content.Context; import android.text.TextUtils; import android.util.AttributeSet; imp ...

  7. sptringboot2.0实现aop

    题记:在项目需要对请求日志情形管理. 声明:参考博客https://blog.csdn.net/bombsklk/article/details/79143145 1.在pom.xml中加入依赖 &l ...

  8. C++中 关于操作符的重载

    C++实现了类的定义,也可以对类之间的操作符进行定义,又叫重载. 例如同类之间的 加.减法,赋值等等操作. 具体看http://blog.csdn.net/zhy_cheng/article/deta ...

  9. 解决 nw 报错 net::ERR_UNSAFE_PORT

    今天 nw 应用里面的前端请求突然不发送了,也没有异常的信息,后来换上开发版 nw 立刻就发现了报错:   net::ERR_UNSAFE_PORT   这个错误的意思很明显,就是请求的 http 端 ...

  10. Matlab中psf2otf()函数在opencv中的实现

    在Matlab中有个psf2otf()函数,可以将小尺寸的点扩散函数,扩大尺寸,并作二维傅里叶变换,opencv中没有这个函数,所以编了这么个函数: /************************ ...