美文网首页
MySql的查询总结

MySql的查询总结

作者: 晓可加油 | 来源:发表于2017-05-22 20:46 被阅读0次

Num01-->mysql基本查询


1、查询所有字段
------

select * from 表名;
例:
select * from students;

2、查询指定字段
------

在select后面的列名部分,可以使用as为列起别名,这个别名出现在结果集中
select 列1,列2,... from 表名;
例:
select id,name,gender from students;

3、消除重复行
-------

在select后面列前使用distinct可以消除重复的行
select distinct 列1,... from 表名;
例:
select distinct gender from students;

Num02-->mysql条件查询


1、使用where子句对表中的数据筛选,结果为true的行会出现在结果集中
-------------------------------------

语法如下:
select * from 表名 where 条件;
例:
select * from students where id=1;

2、where后面支持多种运算符,进行条件的处理,如下
---------------------------

比较运算符
逻辑运算符
模糊查询
范围查询
空判断

2.1比较运算符
--------

等于=
大于>
大于等于>=
小于<
小于等于<=
不等于!=或<>
例1:查询编号大于3的学生
select * from students where id>3;
例2:查询编号不大于4的科目
select * from subjects where id<=4;
例3:查询姓名不是“黄蓉”的学生
select * from students where name!='黄蓉';
例4:查询没被删除的学生
select * from students where isdelete=0;

2.2逻辑运算符
--------

and
or
not
例5:查询编号大于3的女同学
select * from students where id>3 and gender=0;
例6:查询编号小于4或没被删除的学生
select * from students where id<4 or isdelete=0;

2.3模糊查询
-------

like
%表示任意多个任意字符
_表示一个任意字符
例7:查询姓黄的学生
select * from students where name like '黄%';
例8:查询姓黄并且名字是一个字的学生
select * from students where name like '黄_';
例9:查询姓黄或叫靖的学生
select * from students where name like '黄%' or name like '%靖';

2.4范围查询
-------

in表示在一个非连续的范围内
例10:查询编号是1或3或8的学生
select * from students where id in(1,3,8);
between ... and ...表示在一个连续的范围内
例11:查询编号为3至8的学生
select * from students where id between 3 and 8;
例12:查询学生是3至8的男生
select * from students where id between 3 and 8 and gender=1;

2.5空判断
------

注意:null与'' 是不同的
判空is null
例13:查询没有填写地址的学生
select * from students where hometown is null;
判非空is not null
例14:查询填写了地址的学生
select * from students where hometown is not null;
例15:查询填写了地址的女生
select * from students where hometown is not null and gender=0;

3、优先级
-----
优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
and比or先运算,如果同时出现并希望先算or,需要结合()使用

Num03-->mysql聚合函数

为了快速得到统计数据,经常会用到如下5个聚合函数
1、count(*)表示计算总行数,括号中写星与列名,结果是相同的
例1:查询学生总数
select count(*) from students;

2、max(列)表示求此列的最大值
例2:查询女生的编号最大值
select max(id) from students where gender=0;

3、min(列)表示求此列的最小值
例3:查询未删除的学生最小编号
select min(id) from students where isdelete=0;

4、sum(列)表示求此列的和
例4:查询男生的编号之和
select sum(id) from students where gender=1;

5、avg(列)表示求此列的平均值
例5:查询未删除女生的编号平均值
select avg(id) from students where isdelete=0 and gender=0;

Num04-->mysql分组

按照字段分组,表示此字段相同的数据会被放到一个组中
分组后,分组的依据列会显示在结果集中,其他列不会显示在结果集中
可以对分组后的数据进行统计,做聚合运算
语法:
select 列1,列2,聚合... from 表名 group by 列1,列2...
例1:查询男女生总数
select gender as 性别,count(*) from students group by gender;
例2:查询各城市人数
select hometown as 家乡,count(*) from students group by hometown;

分组后的数据筛选
语法:
select 列1,列2,聚合... from 表名 group by 列1,列2,列3...             having 列1,...聚合...

having后面的条件运算符与where的相同
例3:查询男生总人数
方案一
select count(*) from students where gender=1;
-----------------------------------
方案二:
select gender as 性别,count(*) from students group by gender
having gender=1;



where与having的区别
---------------
where是对from后面指定的表进行数据筛选,属于对原始数据的筛选
having是对group by的结果进行筛选

Num05-->mysql排序

为了方便查看数据,可以对数据进行排序
语法:
select * from 表名
order by 列1 asc|desc,列2 asc|desc,...;

将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推
默认按照列值从小到大排列
asc从小到大排列,即升序
desc从大到小排序,即降序

例1:查询未删除男生信息,按学号降序
select * from students where gender=1 and isdelete=0 order by id desc;
例2:查询未删除科目信息,按名称升序
select * from subjects where isdelete=0 order by name;

Num06-->mysql分页

当数据量过大时,在一页中查看数据是一件非常麻烦的事情
语法:
select * from 表名 limit start,count

从start开始,获取count条数据
start索引从0开始

例1:查询前3行男生信息
select * from students where gender=1 limit 0,3;

示例:
已知:每页显示m条数据,当前显示第n页
求总页数:此段逻辑后面会在python中实现
查询总条数p1
使用p1除以m得到p2
如果整除则p2为总数页
如果不整除则p2+1为总页数
求第n页的数据
select * from students where isdelete=0 limit (n-1)*m,m;

Num07-->mysql连接查询

当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回
mysql支持三种类型的连接查询,分别为:
1、内连接查询:查询的结果为两个表匹配到的数据
2、左连接查询:查询的结果为两个表匹配到的数据,左表特有的数据,对于右表中不存在的数据使用null填充
3、右连接查询:查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据使用null填充

语法:
select * from 表1
inner或left或right join 表2 on 表1.列=表2.列

例1:使用内连接查询班级表与学生表
此处使用了as为表起别名,目的是编写简单
select * from classes as cls
inner join students as stu on sub.id=stu.cls.id

例2:使用左连接查询班级表与学生表
select * from classes as cls
left join students as stu on sub.id=stu.cls.id

例3:使用右连接查询班级表与学生表
select * from classes as cls
right join students as stu on sub.id=stu.cls.id

例4:查询学生姓名及班级名称
select stu.name as stuname,cls.name as clsname from classes as cls
inner join students as stu on sub.id=stu.cls.id

Num08-->mysql自关联

设计省信息的表结构provinces
id
ptitle
设计市信息的表结构citys
id
ctitle
proid

citys表的proid表示城市所属的省,对应着provinces表的id值
问题:能不能将两个表合成一张表呢?
思考:观察两张表发现,citys表比provinces表多一个列proid,其它列的类型都是一样的
意义:存储的都是地区信息,而且每种信息的数据量有限,没必要增加一个新表,或者将来还要存储区、乡镇信息,都增加新表的开销太大
答案:定义表areas,结构如下
id
atitle
pid
因为省没有所属的省份,所以可以填写为null
城市所属的省份pid,填写省所对应的编号id
这就是自关联,表中的某一列,关联了这个表中的另外一列,但是它们的业务逻辑含义是不一样的,城市信息的pid引用的是省信息的id
在这个表中,结构不变,可以添加区县、乡镇街道、村社区等信息

创建areas表的语句如下:
create table areas(
aid int primary key,
atitle varchar(20),
pid int
);
从sql文件中导入数据
source areas.sql;
查询一共有多少个省
select count(*) from areas where pid is null;

例1:查询省的名称为“山西省”的所有城市
select city.* from areas as city
inner join areas as province on city.pid=province.aid
where province.atitle='山西省';

例2:查询市的名称为“广州市”的所有区县
select dis.* from areas as dis
inner join areas as city on city.aid=dis.pid
where city.atitle='广州市';

Num09-->mysql子查询

在一个select语句中,可以嵌套另一个select语句,这种查询语句被称为子查询
子查询主要用到三个位置
作为select子名
作为from子句
作为where子句

例1:查询学生姓名及班级名称
select stu.name as stuname,
(select cls.name from classes where cls.id=stu.clsid) as clsname 
from students as stu;

例2:查询学生与班级对应的信息
select * from 
(select stu.*,cls.name as clsname from students as stu 
inner join classes as cls on stu.clsid=cls.id) 
as t1;

例3:查询班级名称为'python1'的所有学生姓名
select stu.name from students as stu
where stu.clsid=(select id from classes where name='python1')

说明:发现很多子查询的语句,都是可以使用连接查询实现的,此时推荐使用连接查询,因为连接查询的语句更简洁,逻辑更清晰

Num10-->mysql完整select语句

select distinct* 
from 表名 
inner 或left或right join  ...on...
where...
group by... having...
order by...
limit start,count;

相关文章

  • MySQL 索引及查询优化总结-2018-03-20

    MySQL 索引及查询优化总结 文章《MySQL查询分析》讲述了使用MySQL慢查询和explain命令来定位my...

  • MySQL慢查询日志总结

    MySQL慢查询日志总结 慢查询日志概念 MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MyS...

  • MySQL--基础二

    本节总结MySQL的筛选条件,聚合与分组,子查询,连接查询。 MySQL的筛选条件 MySQL中的比较运算符: 比...

  • MySQL查询性能优化总结

    MySQL查询性能优化总结 查询执行路径 客户端发送一条查询给MySQL服务器 服务器先检查缓存,如果命中了缓存,...

  • MySql查询-总结

    查询的完整格式 完整的select语句 执行顺序为:from 表名where ....group by ...se...

  • Mysql查询总结

    第一部分:准备数据 1 创建学生表 2 插入数据 3 创建教师表 4 插入数据 5 创建课程表 6 插入数据 7 ...

  • MySql的查询总结

    Num01-->mysql基本查询 Num02-->mysql条件查询 Num03-->mysql聚合函数 Num...

  • 17.MySQL优化

    《高性能MySQL》——这本书都有的 “字段”优化总结 “索引”优化总结 索引的优化 “查询SQL”优化总结 “引...

  • 深入学习MySQL优化

    MySQL高性能优化实战总结 MySQL 的查询过程如下图,很多的查询优化工作实际上就是遵循一些原则。 优化的哲学...

  • MySQL索引知多少

    mysql索引 总结关于mysql的索引,查询优化,SQL技巧等 1 索引类型 B-Tree索引 Hash索引 ...

网友评论

      本文标题:MySql的查询总结

      本文链接:https://www.haomeiwen.com/subject/vouixxtx.html