detachedcriteria查询去重_MySQL系列DML语句之select单表查询

系列文章说明MySQL系列文章包含了软件安装、具体使用、备份恢复等内容,主要用于记录个人的学习笔记,主要使用的MySQL版本为5.7.28,服务器系统版本为CentOS 7.5。本章节为select单表查询内容,本章节使用到了world.sql中的city表和自建的student表,其中world.sql可以在mysql官网下载。注:world数据为19xx年的,数据与现在不匹配。 student表结构如下:

e3b8cbee8102e92688f2e9817e9b620f.png

①sno:学号

②sname:学生姓名

③sage:学生年龄

④ssex:性别

⑤status:状态

city表结构如下:

e21c088a4b3a5b4390f2a0dfaec4bf8a.png

①ID:序号

②Name:城市名称

③CountryCode:国家代码

④District:省/州

⑤Population:人口

关于SELECT

    按照传统的SQL分类,select属于DML,但是由于select作为最常用的语句,有很多人将它和show语句分为DQL。

    无论select是属于DML还是DQL,它都是SQL语言里最重要、最常见的语句,可以配合where、group by、order by等一起使用。

select使用

0 1

单独使用

SELECT

单独使用

①查询全表信息

select * from student;

ef7f789e67d093f8e317d38e776b8e73.png

②查询指定列信息

select sname from student;

4fd21b098957ab6d27d59667ae40b007.png

③计算用途

4a56721624d978c4538d9418a6ba90a0.png

0 2

where条件语句

where语句格式如下:

select 指定列1,指定列2 from 表名 where 条件;

where

比较判断条件查询

常用的比较判断符号包括:>、>=、

①查询sname为yunwei的行信息

select * from student where sname='yunwei';

944962fadc25bab721f15f803bd9b7cc.png

②查询sno小于5的学生姓名

select sno,sname from student where sno<5;

23f5dc67678125c5066abc06b089faba.png

③查询sno为5-10的行数据

select * from student where sno between 5 and 10;

3bb9d0c72f8f36417de0446a5b287b40.png

④查询sno为5或者7的数据

select * from student where sno in(3,5);

6445a8798ad4ab15e8531830f776486d.png

总结

>、

not:非、不

in、not in :在、不在

where

like模糊查询

①查询名字列包含zhang的学生姓名行

select * from student where sname like "%zhang%";

dcb4f86024b9e9a54f83640716135aef.png

②查询sname列以a开头的行

select * from student where sname like "a%";

1eae5054a25a924cbe259641b36e404c.png

③查询sname列以i结尾的行

select * from student where sname like "%i";

4379bae0e37d0d45a9012ed9e82445b1.png

④查询sname列第二个字符为h的列

select * from student where sname like "_h%";

7acf24b9150416c96cafca90f0c7ac28.png

⑤查询sname列第二个字符不为h的列

 select * from student where sname not like "_h%";

c17c019d710ac0df1358dd9bab82092c.png

like通配符

%:匹配零个或者多个字符

_:匹配一个字符

where

逻辑连接符

逻辑连接符号主要包括:and、or、union

①查询中国人口超过100w人口的城市信息

select * from city where countrycode='CHN' and population>1000000;

2278bf42d0c004e17efec672b792df95.png

②查询广东或者广西的城市信息

select * from city where district='guangdong' or district='guangxi';

d66aaded39d2d4588472fca4d0faf0a3.png

select * from city where district='guangxi';

5dd6839503e82d60715a494beb524853.png

总结

and:与,两个条件同时满足的数据会被选中

or:或,满足一个或一个以上的数据会被选中

union:类似或,上下两个语句满足一个即选中

0 3

group by

    group by在绝大多数的情况下需要与where配合使用,group by主要适用于分组,group by一定需要配合聚合函数,常用的聚合函数有:max(),min(),avg(),count(),sum(),group_concat()、DISTINCT()

    group by的核心使用步骤:

    ①找出分组条件(哪个列作为分组列)

    ②使用适合的聚合函数(怎么计算)

group by

sum总和

①计算每个国家的人口数量

select CountryCode,sum(Population) from city group by countrycode;

142a7390f46f10ac5e90fb91e127bdf9.png

②计算中国每个省的人口总数

# 由于省的人口总数=sum(城市人口) 有重复列的为省,所以分组列应该为省select District,sum(population) from city where countrycode='CHN' group by district;

659605ca7027a7aec68d9bcda5508c23.png

group by

count计数

①统计每个国家的城市数量

select CountryCode,count(district) from city group by Countrycode;

4c6160612eccd9ece73673de40cae692.png

②统计中国每个省的城市数量

select District,count(name) from city where countrycode='CHN' group by district;

140e548475fa4a5a9f26ae651117d248.png

0 4

having

    having与where类似,having属于后过滤,一般需要在group by + 聚合函数后,再做过滤时使用。

having

having使用

①统计中国每个省的总人口,只显示总人口大于500w的省

select District,sum(population) from city where countrycode='CHN' group by district having sum(population)>5000000;

200f29480a793e1548ab76d9fe6ed404.png

0 5

order by排序

    order by排序,order by 排序的列 DESC 倒序 ,没有DESC为正序。

order by

排序

①统计中国每个省的总人口,只显示人口数大于500w信息,并且按照少到多排序

select District,sum(population) from city where countrycode='CHN' group by district having sum(population)>5000000 order by sum(population);

58cfacf5083547c145eba89b29182714.png

0 6

limit分页

    limit分页显示结果集 一般配合order by使用,limit x -- 前x个,x,y表示开始为x,往再显示后面y个。

limit

limit分页

①统计中国每个省的总人口,只显示人口数大于500w信息,并且按照多到少排序,只显示前五名

select District,sum(population) from city where countrycode='CHN' group by district having sum(population)>5000000 order by sum(population) DESC limit 5;

85d3a025a8a9e35d8d4f09c7cc4de572.png

0 7

别名和去重

    别名:给列取一个别名,有时候列名太长,可以取个简单易懂的别名做后续的处理和展示。

    去重:去掉重复的值,重复的只显示一个

select

别名

    给列取别名,可以在列后面直接添加一个别名,或者使用as 添加一个别名

select countrycode '国家代码',name as '城市名' from city where district='guangdong';

5ab67dad98c3bd0f477019fe456476b4.png

select

去重

    distinct是select中去重语句

①打印出所有的国家代码,且不重复

select distinct(countrycode) from city;

fdb7980cffe3d4dc90d9adc0e765e006.png

0 8

案例说明

   

    统计中国,每个省总人口,城市个数,城市名列表

SELECT District,SUM(Population),COUNT(id),NAME FROM world.`city` WHERE CountryCode='CHN' GROUP BY District;

    如果你也是这样做的,恭喜你,你会收到一个报错信息如下:

ERROR 1055 (42000): Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'world.city.Name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

    这是什么原因呢?首先我们看看我们上面的指令想做出来的结果是怎么样的

省/州总人口城市个数城市名称

广东省

10000000


10
广州
深圳
东莞
...

    但是!mysql不支持一对多的结果显示,那我们怎么处理呢?这时候就可以使用到聚合函数group_concat,对多行数据转换为一行,达到一行对一行的效果。

省/州总人口城市个数城市名称
广东省1000000010广州,深圳,东莞...
 SELECT District,SUM(Population),COUNT(id),group_concat(NAME) FROM world.`city` WHERE CountryCode='CHN' GROUP BY District;

e89e2bcc4cf524a017d0f47ec5ede7c0.png

附个人思维导图

6e075a030a26887a3b543a094dfc0ee0.png

end

7289d4ce9bc205ee5733b5560f39abe4.png

7830952366756168967021179594537b.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值