week06 day02(sql gropy by 、having、order by、limit、运算符、函数)

一.函数

1.group by

  • 按照某一字段进行分组, 会把该字段中值相同的归为一组, 将查询的结果 分类显示, 方便统计。
  • 如果有 WHERE 要放在 WHERE 的后面
  • 语法: select 字段 from 表名 group by 分组字段;
注意:group by 一般是配合统计函数使用的,如果没有使用统计函数,每个分组只会挑选第一个数据。例如sex = 男中,郭德纲是在第一个,所以选他
sql 复制代码
select *
from student
group by `sex`

2. count 和 with rollup

sql 复制代码
select sex, count(*)
from student 
GROUP BY sex
with rollup: 增强统计(在分组统计的基础上,再求和一次)
sql 复制代码
select sex, count(*)
from student 
GROUP BY sex
with rollup

3. Having

  • 语法: SELECT 字段 FROM 表名 HAVING 条件
  • 条件字段必须要在结果集中出现 , HAVING 可以写在 GROUP BY 的后面
临时表:临时表必须要 as 别名, 临时表可以作为数据源

因为select 在sql执行中,在where和having之后,所以你给别名age,在where或者having进行条件筛选时没有作用,创建临时表,可以有效解决这一问题

sql 复制代码
(select 
	`name`, 
	`sex`, 
	`city`,
	money,
	year(NOW()) - year(birthday) as age
from student
	)as temp
按照city分组,且选择计数大于1 的城市

这里用了 临时表, group by 分组, having 对分组后的结果进行进一步筛选

sql 复制代码
select  city as 城市,COUNT(*) as 计数
from
	(select 
	`name`, 
	`sex`, 
	`city`,
	year(NOW()) - year(birthday) as age
from student
	)as temp
where age < 30
group by city
HAVING 计数 >1

4. order by 排序(正序,倒序,随机排) (运行顺序在select 之后)

sql 复制代码
order by () asc   正序
order by () desc  倒序
order by sex,city desc  会先按照sex 降序,再按照 city降序
order by rand() 随机排序

select  city as 城市,COUNT(*) as 计数
from
	(select 
	`name`, 
	`sex`, 
	`city`,
	year(NOW()) - year(birthday) as age
from student
	)as temp
where age < 30
group by city
order by 计数 asc # 正序

5. limit 限制取出数量

sql 复制代码
select 字段 from 表名 limit m;  -- 从第 1 个到第 m 个
select 字段 from 表名 limit m, n;  -- 从第 m 个开始,往下取n 个
select 字段 from 表名 limit m offset n;  -- 跳过前 n 个, 取后面的 m 个

6. LIKE (模糊查询) regexp语法与like类似(没有? 贪婪模式)

在模糊查询中,_ 代表的是匹配一个字符串, %代表是0~多位

sql 复制代码
select * 
from student 
where `name` like "郭%"

7. ROUND(X,2) 保留几位小数

sql 复制代码
SELECT ROUND(2.333,2)

8. concat() 字符拼接

sql 复制代码
select 
CONCAT("123","abc")

-- 123abc

9. 去重 distinct()

城市有重复,看一共有哪些城市

sql 复制代码
select
count(DISTINCT city)
from student

10. group_concat()

当不使用group_concat时,结果如下图,上海后的name只有一个人名字

sql 复制代码
select city,`name`
from student
group by city
sql 复制代码
select city,GROUP_CONCAT(`name`)
from student
group by city

11. MOD(X,Y)

返回 x/y 的模

二. 运算符

1. xor 逻辑异或:

  • (TRUE,TRUE) FALSE
  • (TRUE,FALSE) TRUE
  • (FALSE,TRUE) TRUE
  • (FALSE,FALSE) FALSE

三. 日期函数

date_format
sql 复制代码
-- 1997-10-01
select DATE_FORMAT(birthday,"%Y-%m-%d") FROM STUDENT
date_add
sql 复制代码
select DATE_ADD("2019-8-8",INTERVAL 1 YEAR) from student

四. 字符串函数

相关推荐
qq_206901394 分钟前
如何创建CDB公共用户_C##前缀强制规则与CONTAINER=ALL.txt
jvm·数据库·python
code bean7 分钟前
MySQL 远程访问实战:从基础操作到真实踩坑记录
数据库·mysql
Hello World . .8 分钟前
Linux驱动编程:内核同步的艺术-从互斥到底半部
linux·开发语言·数据库
Go 言 Go 语8 分钟前
Claude Code 核心加载机制详解
服务器·前端·数据库
weixin_568996068 分钟前
golang如何实现多活架构方案_golang多活架构方案实现教程
jvm·数据库·python
Absurd58711 分钟前
Golang map遍历顺序为什么随机_Golang map遍历原理教程【进阶】
jvm·数据库·python
FinTech老王12 分钟前
突破批处理瓶颈:KingbaseES并行DML技术如何榨干多核CPU性能
数据库·安全·oracle
2301_8038756113 分钟前
Golang怎么实现WebSocket房间_Golang如何按房间分组管理不同的连接群组【方法】
jvm·数据库·python
2301_7965885014 分钟前
Golang怎么用Task替代Makefile_Golang如何用go-task编写跨平台的任务脚本文件【教程】
jvm·数据库·python
悟空爬虫-彪哥18 分钟前
Stich接入Codex教程
java·前端·数据库