文章目录
(1)group by 之前,只能用 group by 的字段,select 之后
前言
SQL是结构化查询语言(Structured Query Language)的缩写,是一种用于管理和操作关系型数据库的语言。它可以用于创建、修改和删除数据库中的表格和数据,以及执行查询和分析数据的操作。
MySQL是一种流行的关系型数据库管理系统,它使用SQL作为查询和管理数据库的语言。
如何使用MySDL进行查询?
-
连接数据库:使用MySQL客户端工具(如MySQL Workbench)或编程语言中的MySQL库,连接到的数据库服务器。
-
选择数据库:在连接到数据库后,选择要查询的数据库。使用"USE"语句指定要使用的数据库。例如,使用"USE dbname;"来选择名为"dbname"的数据库。
-
编写查询语句:使用SQL语法编写查询语句。查询语句的类型包括SELECT(用于检索数据)、INSERT(用于插入数据)、UPDATE(用于更新数据)和DELETE(用于删除数据)。根据您的需求,编写适当的查询语句。
-
执行查询:将查询语句发送到数据库服务器以执行查询操作。在MySQL客户端工具中,可以直接在查询编辑器中执行查询语句。在编程语言中,您可以使用相应的MySQL库提供的函数来执行查询。
-
处理查询结果:获取查询结果,并根据您的需求进行处理。根据查询的类型,查询结果可以是检索到的数据、受影响的行数或其他相关信息。
一、SDL查询是什么?
语法结构:select-from-where-group by-hanving-order by-limit
运行顺序:from-where-group by-having-order by-limit-select
二、必备工具
在线练习数据库
- world:https://sqlzoo.net/wiki/SELECT from WORLD Tutorial
- nobel:https://sqlzoo.net/wiki/SELECT from Nobel Tutorial
- world:https://sqlzoo.net/wiki/SUM and COUNT
- covid:https://sqlzoo.net/wiki/Window LAG
阅读并理解world 和nobel 表
三、知识点
1.select 查找
- 基础用法:select 字段名 from 表名
- 别名语法:select 字段名 as 别名 from 表名(as可以省略)
- 查询多列:select 字段名1,字段名2 from 表名
- 查询所有列:select* from 表名
select name as 国家名, continent 大洲
去重:在 select distinct ,对重复的行数据去重
select distinct continent 大洲 from world
只返回八大州
计算字段(四则运算,可以嵌套)
人均GDP=gdp/population
select name,gdp,population,gdp/population 人均GDP from world
2.where 行数据的限定
(1)where + 比较运算符
人口大于等于20000
select name,gdp,population,gdp/population 人均GDP from world
where population>=20000
(2)where + 逻辑运算符
基础语法:where =‘某个值’
where name='Germany'
(3)多条件 in的用法
in 取列表,筛选所有与括号内数据相等的行
where name in ='
select population,name from world
where name in ('','','')
(4)between and 用法
between number1 and number2
介于两个值之间的数据(从小到大)
(!=)去掉边界,其实就是不等于
select name,area from world
where area between 1 and 20000
and area != 20000
(5)近似查找
where like ('')
%任意字符
_占位符
select name
from world
where name like 'C%ia'
第二个字符为t的国家名
select name
from world
where name like '_t%'
只要有a的国家名
select name
from world
where name like '%a%'
有oo且被两个字符隔开
select name
from world
where name like '%o__o%'
多条件查询 and 同时满足 或者or 满足一个
and 优先于 or
可用()标记优先
名称包含aaa且面积大于60w
select name,area
from world
where name like '%a%a%a%'and area >= 600000
3.order by 排序
(1)升序:order by asc(默认升序)
(2)降序:order by desc
多条件排序:按照字段顺序排序,都要放在order by 之后。order by第一个条件 排序方式,第二个条件 排序方式
(3)特殊排序:
order by 字段 in (条件1,条件2),满足()内条件则返回1,不满足返回0
例1:Show the 1984 winners and subject ordered by subject and winner name; but list chemistry and physics last.(The expression subject IN ('chemistry','physics') can be used as a value - it will be 0 or 1.)
SELECT winner,subject from nobel
WHERE yr=1984
ORDER BY subject in ('chemistry','physics'),subject,winner
4.limit 取前几个
(1)取前几
limit n
select * from world
order by area desc
limit 3
(2) 从x+1行开始返回n行
limit x,n
例1:第4-7行 limit 3,4
例2:查询nobel表中100-120行数据
select * from nobel
limit 99,20
5.聚合函数 和 group by
(1)聚合函数的是数值,会忽略空值
-
返回某列的和:sum()
例:求非洲总人口
SELECT continent, sum(population) FROM world
where continent = 'Africa'
-
计算表格行数 count(*),不管是否有空值
-
计算字段行数 count(字段),如果空值会被忽略
SELECT count(*),count(gdp),count(name),count(area),count(continent) FROM world
-
某列最小值 min()
-
某列最大值 max()
- 某列均值 avg(),忽略空值
(2)group by
根据哪个字段聚合,提供聚合依据,将分区去重分组,打破原表的结构,创建新的表
- 单独使用group by
例:按大洲计算国家数
SELECT continent,count(*) FROM world
group by continent
(3)聚合函数和group by联用:
group by(字段1,字段2),依照字段顺序聚合(列)
注:先分类再汇总排序,select是最后一个运行,只选择展现or 不展现,如果聚合依据或者排序字段在select中没有,将会报错!!
例:2013到2015各学科获奖人数,按照年份、人数降序排列
SELECT yr, subject, count(winner) 获奖人数
FROM nobel
WHERE yr between 2013 and 2015
group by yr,subject
order by yr desc,count(winner) desc
6.having 聚合后筛选
(1)group by 之前,只能用 group by 的字段,select 之后
(2)与where 区别:where 聚合前筛选
对国家的限制:where 其中......
对大洲的聚合:having 总人口数大于3亿
select continent,gdp,avg(gdp),count(name) 国家数,capital from world
where (gdp > 200000000000 and population >60000000)
or (gdp < 80000000000 and capital like '%a%a%a%')#gdp大于200亿且人口大于60亿,或者gdp小于80亿,首都名有三个a
group by continent#按大洲聚合
having sum(population) > 3000000000#总人口大于3亿
order by count(name) desc#按名称降序排列
limit 1#只显示第一行
7.其他常见函数
(1)数学类型
- round(x,y),对x四舍五入,精确到小数点后y位。round(3.15,2) 返回3.2
- concat(s1,s2)返回 s1s2
- replace(原有,替换什么,替换成什么)replace(name,a,A) 返回 nAme
- left(字符串,从左开始截取到第几个)
- right(字符串,从右开始截取到第几个)
- substring(字符,从第几个开始,截取多长)
(2)数据类型转换
- cast(原有字段 as type),type类型可以是year(date)、month(date).day(date)
- date_add(起始时间,interval 增加的时间)
- date_sub()
- 两个日期间隔天数:datediff(日期1,日期2)
- 日期格式化:date_format ('2024-4-13','%b %d %m %Y')返回Apirl 13 04 2024
(3)条件判断函数
- if(表达式,满足返回什么,不满足返回什么)
- 嵌套条件 case when
case 原有字段 when 后面字段 then 返回什么 [以此类推] else 剩下返回什么 end
举例:case 2 when 1 then 'one' when 2 then 'two' else 'more' end 返回two
四、练习题目
1.
2. like 和 like in 的区别
like
like in 相当于 or
3.not in ('','')
4.多条件
5.聚合函数和group by
6.having
查询人均GDP大于3000的大洲及人口数
7.left