0. 准备数据

-- 创建数据库
create database db_python charset=utf8;-- 使用数据库
use db_python;-- students表
create table students(id int unsigned primary key auto_increment not null,name varchar(20) default '',age tinyint unsigned default 0,height decimal(5,2),gender enum('男','女','中性','保密') default '保密',cls_id int unsigned default 0,is_delete bit default 0
);-- classes表
create table classes (id int unsigned auto_increment primary key not null,name varchar(30) not null
);
-- 向students表中插入数据
insert into students values
(0,'小明',18,180.00,2,1,0),
(0,'小月月',18,180.00,2,2,1),
(0,'彭于晏',29,185.00,1,1,0),
(0,'刘德华',59,175.00,1,2,1),
(0,'黄蓉',38,160.00,2,1,0),
(0,'凤姐',28,150.00,4,2,1),
(0,'王祖贤',18,172.00,2,1,1),
(0,'周杰伦',36,NULL,1,1,0),
(0,'程坤',27,181.00,1,2,0),
(0,'刘亦菲',25,166.00,2,2,0),
(0,'金星',33,162.00,3,3,1),
(0,'静香',12,180.00,2,4,0),
(0,'郭靖',12,170.00,1,4,0),
(0,'周杰',34,176.00,2,5,0);-- 向classes表中插入数据
insert into classes values (0, "python_01期"), (0, "python_02期");

1.普通查询

1.1 查询所有字段

-- select * from 表名;select * from students;

1.2 查询指定字段

-- select 列1,列2,... from 表名;select name,age from students;

1.3 使用 as 给字段起别名

-- select 字段 as 名字.... from 表名;select name as '姓名',age as '年龄'  from students;

1.4 可以通过 as 给表起别名

-- select 别名.字段 .... from 表名 as 别名;select s.name as '姓名',s.age  from students as s;

1.5 消除重复行(查性别)

-- 在select后面,列的前面使用distinct可以消除重复的行
(去重)select distinct gender from students;

1.6 删除小明同学

-- 物理删除/硬删除 (通过delete from 把记录从表中删除)delete from t_students where id=4;
-- 逻辑删除/软删除(用一个字段来表示 这条信息是否已经不能再使用了)update students set is_delete=1 where id=1;

2.条件查询

select … from 表名 where 条件

2.1 比较运算符 > 、< 、>=、<=、=、!=

-- >
-- 查询大于18岁的学生信息select * from students where age>18;
-- <
-- 查询小于18岁的学生信息select * from students where age<18;
-- >=
-- <=
-- 查询小于或者等于18岁的学生信息
select * from students where age<=18;
-- =
-- 查询年龄为18岁的所有学生的名字
select * from students where age=18;
-- != 或者 <>
select * from students where age!=18;

2.2 逻辑运算符 and、or、not

-- and  (并且) 年龄在18和28之间的所有学生信息
select * from students where age>18 and  age<28;-- 18岁以上的女性
select * from students where age>18 and  gender='女';-- or (或者)
-- 18以上或者身高高过180(包含)以上的学生信息select * from students where age>18 or height>=180;-- not (取反)
-- 不在 18岁以上的女性 这个范围内的信息
select * from students where (not age>18)  and gender='女';

2.3 模糊查询(where 列名 like 要查询的数据)

-- like
-- % 替换任意个字符
-- _ 替换1个字符   -- 查询姓周的学生
select * from students where name like '周%';-- 查询姓名中 包含 "杰" 字的所有名字
select * from students where name like '%杰%';-- 查询姓周并且“名”是一个字的学生
select * from students where name like '周_';-- 查询姓黄或叫靖的学生
select * from students where name like '黄%' or  name like '%靖%';-- rlike 正则
-- 查询以 周开始的姓名select * from students where name rlike '^周.*';select * from students where name rlike '杰.*';

2.4 范围查询

-- in 表示在一个非连续的范围内
-- 查询编号是1或3或8的学生select * from students where id in (1,3,8);select * from students where name in ("小明","小月月");-- between ... and ...表示在一个连续的范围内
-- 查询 年龄在18到34之间的男同学信息
select * from students where age between 18 and 34;-- 查询 年龄不在在18到34之间的的信息-- 语法: not between ... and ... : 不再范围内select * from students where age not between 18 and 34;select * from students where not (age between 18 and 34);

2.5 空判断

-- is null (判空)
-- 查询身高为空的学生信息
select * from students where height is null;
-- is not null(判非空)
-- 查询身高不为空的学生信息
select * from students where height is not null;
select * from students where not (height is null);

2.6 优先级

--优先级由高到低的顺序为:小括号,比较运算符,逻辑运算符
--not>and>or,如果同时出现并希望先算or,需要结合()使用
--分析以下sql语句:
select * from students where  not age>=18 or height>=180 and gender="男";

3.排序

select * from 表名 [where …] order by 列1 asc|desc [,列2 asc|desc,…]
​ – 有where条件时,order by需放到where条件的后面
​ – asc从小到大排列,即升序,默认是升序
​ – desc从大到小排序,即降序

3.1 order by 单个字段字段

-- 查询未删除学生的信息,按年龄降序排序select * from students where is_delete=0  order by  age desc;

3.2 order by 多个字段

-- 查询所有学生信息,按照年龄从小到大、当年龄相同,则身高从高到矮的排序
select * from students  order by  age asc, height desc;
select * from students  order by  age , height desc;

4.聚合函数

对查询的数据结果集进行统计分析

4.1 求总数 count

 -- count(*)表示计算总行数-- 统计学生表中的总人数select count(*)  from students;-- count(列)表示计算某列的总行数,假如该列某个值为null则不会统计-- 统计学生表中,height不为空的总人数select count(height)  from students;

4.2 求最大值 max

-- max(列) 求此列的最大值
-- 查询最大的年龄select max(age) from students ;-- 查询女性的最高身高
select max(height) from students  where gender='女';

4.3 求最小值 min

-- min(列)表示求此列的最小值
-- 查询最小身高select min(height) from students ;

4.2 求和 sum

-- sum(列)表示求此列的和
-- 计算所有学生的年龄总和select sum(age) from students ;

– 5.5 求平均值

-- avg(列)表示求此列的平均值
-- 计算未删除女生的总人数及平均年龄
select count(*),avg(age) from students where is_delete=0 and  gender='女' ;

5.分组

– 在实际业务中,经常会对数据进行分类统计操作,通过 group by 可实现分组,做更精细的聚合统计操作
– select * from tbname where 条件 group by…having 条件…

5.1 group by

-- 将查询结果按照1个或多个字段进行分组,字段值相同的为一组
--注意: select 列 中只能存放分组函数(比如聚合函数),或是出现在group by子句中的分组标签-- 按照性别分组,查询所有的性别--去重方式select distinct gender from students;--分组方式select gender from students group by gender;

5.2 group by + 聚合函数

-- 查询每组性别的平均年龄select gender,avg(age) from students group by gender;-- 查询每种性别中的平均年龄avg(age), 最大年龄,平均身高,最高身高select gender,avg(age) as '平均年龄',max(age),avg(height),max(height) from students group by gender;

5.3 group_concat(列)

-- group_concat(字段名) 拼接字符串, 可以作为一个输出字段的值来使用
-- 表示分组之后,根据分组结果,使用group_concat()来拼接每一组的某字段的值-- 查询同种性别中的姓名select gender, group_concat(name), max(age) from students group by gender;

5.4 having条件

-- having 条件表达式:对分组后的数据进一步筛选
-- 除了男生以外的分组的人数select gender,count(*) from students group by  gender having gender!='男';-- 查询每种性别中的平均年龄avg(age)select gender,avg(age) from students group by gender;-- 按性别分组,分别统计出平均年龄超过30岁的组的性别以及姓名 having avg(age) > 30
select gender,group_concat(name) from students group by gender having avg(age)>30;-- having 和 where 的区别
where: 对源数据做条件筛选,where 条件中 不能接聚合函数
having: 是对分组之后的数据做进一步的筛选操作有having就一定有group by, 有 group by 不一定有havinghaving条件中 可以接聚合函数,而where不行

6. 分页

– 当数据量过大时,通过分批、分页加载数据既能提升加载速度,也可更好显示查询结果
​ – select * from 表名 limit [start,] count
​ – 限制查询,start为查询的起始位置,count为限制查询的数量
​ – 注意: limit 放在查询语句的最后面 限定

-- 查询前5个数据select * from students limit 5;select * from students limit 0,5;-- 分页查询
-- 每页显示2个,第1个页面select * from students limit 0,2;
-- 每页显示2个,第2个页面
select * from students limit 2,2;
-- 每页显示2个,第3个页面
select * from students limit 4,2;
-- 每页显示2个,第4个页面
select * from students limit 6,2;-- 每页显示2个,显示第6页的信息, 按照年龄从小到大排序
-- 公式: start=(page-1)*countselect * from students order by age  limit 10,2;

7.连接查询

7.1 inner join … on (内连接)

– 结果仅包含符合连接条件的两表中的行
– select … from 表A inner join 表B on 条件;

-- 查询有能够对应班级的学生以及班级信息    select * from students inner join classes on students.cls_id=classes.id;-- 只显示姓名、班级
select students.name,classes.name from students inner join classes on students.cls_id=classes.id;-- 给数据表起名字
select s.name as '姓名',c.name as '班名' from students as s inner join classes as c on s.cls_id=c.id;

7.2 left join … on (左连接)

– 结果仅包含符合连接条件的两表中的行
– select … from 表A left join 表B on 条件;
左表全部行+右表匹配的行,如果左表中某行 在右表中没有匹配的行,则右表该行显示NULL

-- 查询每位学生对应的班级信息
select * from students as s left join classes as c on s.cls_id=c.id;-- 查询没有对应班级信息的学生
-- select ... from xxx as s left join xxx as c on..... where .....
select * from students as s left join classes as c on s.cls_id=c.id where c.name is null;

7.3 right join … on (右连接)

– 结果仅包含符合连接条件的两表中的行
– select … from 表A right join 表B on 条件;
– 右表全部行+左表匹配的行,如果右表中某行 在左表中没有匹配的行,则左表该行显示NULL

-- 查询没有对应班级信息的学生
select * from classes as c right join students as s on s.cls_id=c.id;

8.子查询 : 查询嵌套

8.1 标量子查询

标量子查询返回的结果是一个数据(一行一列)

-- 查询出高于平均身高的信息(height)
-- 1 查出平均身高select avg(height) from students;-- 2 查出高于平均身高的信息
select * from students where height>(select avg(height) from students);

8.2 列子查询

列子查询返回的结果是一列(一列多行)

-- 查询有班级的学生信息
-- select name from students where cls_id in (select id from classes);
-- 1 查出所有的班级idselect id from classes;-- 2 查出能够对应上班级号的学生信息select * from students where cls_id in (select id from classes);

8.3 表子查询

表子查询返回的结果是一个临时表(多行多列)

-- 查询编号小于6的男性同学的姓名select name from (select * from students where id<6) as s  where gender='男';-- select name from (select gender,name from students where id<6) as s where gender="男";

9. 自关联查询

-- 创建areas表(aid,atilte,pid)create table areas(aid int primary key,atitle varchar(20),pid int);
-- 向areas插入记录source areas.sql;
-- 查询一共有多少个省select count(*) from areas where pid is null;
-- 查询广东省中的所有城市
--select city.* from areas as city inner join areas as province on city.pid=province.aid where province.atitle='广东省';-- 查询属于深圳市的所有的区
--select a.* from areas as a inner join areas as c on a.pid=c.aid where c.atitle='深圳市';

10.总结

SELECT select_expr [,select_expr,...] [      FROM tb_name[WHERE 条件判断][GROUP BY {col_name | postion} [ASC | DESC], ...] [HAVING WHERE 条件判断][ORDER BY {col_name|expr|postion} [ASC | DESC], ...][ LIMIT {[offset,]rowcount | row_count OFFSET offset}]
]

完整的select语句

select distinct *
from 表名
where ....
group by ... having ...
order by ...
limit start,count

执行顺序为:

from 表名
where ....
group by ...
select distinct *
having ...
order by ...
limit start,count

MySQL的查询(超详细附带数据供练习)相关推荐

  1. mysql在查询结果中添加数据_MySQL将查询结果插入到数据表中

    转自Mysql教程:http://www.baike369.com/content/?id=5582 MySQL将查询结果插入到数据表中 INSERT语句还可以将SELECT语句查询出来的数据插入到另 ...

  2. mysql查询哪天,Mysql日期查询的详细介绍

    查询当前日期 SELECT CURRENT_DATE(); SELECT CURDATE(); 查询当前日期和时间 SELECT NOW(); 查询今天的数据 SELECT * FROM `表名` W ...

  3. 学习pandas全套代码【超详细】数据查看、输入输出、选取、集成、清洗、转换、重塑、数学和统计方法、排序

    大家早上好,本人姓吴,如果觉得文章写得还行的话也可以叫我吴老师.欢迎大家跟我一起走进数据分析的世界,一起学习! 感兴趣的朋友可以关注我的数据分析专栏,里面有许多优质的文章跟大家分享哦. 本篇博客将会给 ...

  4. 一份超详细的数据科学路线图!

    ↑↑↑关注后"星标"Datawhale 每日干货 & 每月组队学习,不错过 Datawhale干货 作者:魔王.陈萍,来源:机器之心 从头开始学习数据科学的免费资源. 如何 ...

  5. 云服务器(云数据库)连接Mysql数据库【超详细-少踩雷】

    连接Mysql和sql server数据库 一. 华为云服务器 1.云服务器配置 2.使用远程桌面连接 二.安装配置MySQL可视化软件 三.Android Studio 代码片段 所有代码,直接贴出 ...

  6. mysql+sqlyog的超详细完整安装+数据库基础知识

    目录 一.安装mysql 下载 解压添加环境变量 创建文件 启动mysql 安装mysql 二.sqlyog安装 sqlyog下载 三 补充 3.1 常用的数据库命令 3.2 基本操作 3.2.1 常 ...

  7. 大数据hadoop、mysql、hive超详细配置及问题解决

    1.前提准备 hadoop-3.1.3.tar.gz jdk-8u162-linux-x64.tar.gz mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz apa ...

  8. mysql 条件查询分页_百万数据下mysql条件查询及分页查询的注意事项

    接上一节<百万数据mysql分页问题>,我们加上查询条件:select id from news where cate = 1 order by id desc limit 500000 ...

  9. (超详细)大数据Hadoop之MapReduce组件

    一.MapReduce 简介 1.1 MapReduce的概述 在Hadoop生态圈中,MapReduce属于核心,负责进行分布式计算. MapReduce 核心功能是将用户编写的业务逻辑代码和自带默 ...

最新文章

  1. stm32编码器正反转计数程序_编码器接线方法你会吗?
  2. 黑盒测试和白盒的区别,有哪些常见的白盒黑盒测试方法
  3. 16位的数字高字节和低字节_显示8位数字的较低和较高半字节的掩蔽| 8086微处理器...
  4. 快速排序查询第k大元素C语言,快速排序和查找第K大元素
  5. Tomcat组件梳理—Digester的使用
  6. 2019大学生电子设计竞赛
  7. kong翻译_最全的中国姓氏英文说法,你知道自己的姓氏怎么翻译吗?
  8. xXx时间屏保+背景图片
  9. 刚刚才发现,原来这四款软件可以厉害到这种程度
  10. 为Halide安装opencl支持
  11. InoReader—— 轻便快捷的在线 RSS 阅读器
  12. 计算机任务无法结束,简单几步解决win7任务管理器无法结束进程的问题
  13. 腾讯云数据库 TDSQL-之初体验
  14. 36-Java方法的案例:求和、判断奇偶数、求最值
  15. 拖延是一种病----你有么?
  16. 利用淘宝IP查询接口,免费查询IP归属地
  17. k8s实践(1)--k8s集群入门介绍和基础原理
  18. 刘德华被警方传讯 竟在派出所内“顺走”警察手机
  19. 基于工业5G网关的建筑机器人应用
  20. 快速找一个数的所有因子

热门文章

  1. android智能识物
  2. 大广角镜头 镜头畸变 矫正_如何用广角镜头对焦
  3. 入耳式耳机哪个牌子的音质比较好?学生平价蓝牙耳机推荐
  4. 计算机专业迎新标语,2018迎新标语大全 这样的脑洞给跪了
  5. 程序员与产品经理掐架图鉴
  6. win10鼎信诺为什么安装不了_阳光系统 win10显卡驱动安装失败怎么办
  7. html中空心圆圈怎么表示,html5使用canvas画空心圆与实心圆
  8. 珍爱生命,远离小米手机(上)
  9. 苹果财报即将发布,还有多少坏消息来袭?
  10. linux部署tomcat与快速启动