1.截取字符串 substr(字段, 1, n)
-- 下标从1开始
-- 从第4个字符,截取长度为2的子串
select substr("hello world",4,2);
select substr("hello world",1,2);
2.大写 upper(...)
select upper("hello");
3.字符串拼接 concat(...)
select "hello";
select "world";
-- "hello" + " " + "world"
select concat("hello", " ", "world");
4.时间 datetime
select "2023-04-22 11:33:29";
select year("2023-04-22 11:33:29");
select month("2023-04-22 11:33:29");
select day("2023-04-22 11:33:29");
select hour("2023-04-22 11:33:29");
select minute("2023-04-22 11:33:29");
select second("2023-04-22 11:33:29");
--判断两天之间差多少天 前面减去后面
select DATEDIFF('2023-04-22', '2023-05-01');
例题
--SQL82 返回 2020 年 1 月的所有订单的订单号和订单日期 牛客
--https://www.nowcoder.com/practice/c7734db33854477aa94ae238a3390435?tpId=298&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D298
--中等 通过率:48.48% 时间限制:1秒 空间限制:256M
--描述
--Orders订单表
--order_num order_date
--a0001 2020-01-01 00:00:00
--a0002 2020-01-02 00:00:00
--a0003 2020-01-01 12:00:00
--a0004 2020-02-01 00:00:00
--a0005 2020-03-01 00:00:00
--【问题】编写 SQL 语句,返回 2020 年 1 月的所有订单的订单号(order_num)和订单日期(order_date),并按订单日期升序排序
--【示例结果】
--返回订单号order_num,和order_date订单时间
--order_num order_date
--a0001 2020-01-01 00:00:00
--a0003 2020-01-01 12:00:00
--a0002 2020-01-02 00:00:00
--【示例解析】
--a0001、a0002、a0003 时间属于2020年1月
--示例1
--输入:
--DROP TABLE IF EXISTS `Orders`;
--CREATE TABLE IF NOT EXISTS `Orders`(
-- order_num VARCHAR(255) NOT NULL COMMENT '订单号',
-- order_date TIMESTAMP NOT NULL COMMENT '订单日期'
--);
--INSERT `Orders` VALUES ('a0001','2020-01-01 00:00:00'),
--('a0002','2020-01-02 00:00:00'),
--('a0003','2020-01-01 12:00:00'),
--('a0004','2020-02-01 00:00:00'),
--('a0005','2020-03-01 00:00:00');
--复制
--输出:
--a0001|2020-01-01 00:00:00
--a0003|2020-01-01 12:00:00
--a0002|2020-01-02 00:00:00
--第一种写法
select
order_num,
order_date
from
Orders
where
year(order_date) = 2020
and
month(order_date) = 1
order by
order_date asc;
--第二种写法
select
order_num,
order_date
from
Orders
where
order_date < '2020-02-01 00:00:00'
and
order_date >= '2020-01-01 00:00:00'
order by
order_date asc;
--第三种写法,其实和第二种写法差别不大
select
order_num,
order_date
from
Orders
where
order_date
between
'2020-01-01 00:00:00'
and
'2020-01-31 23:59:59'
order by
order_date;
5.聚合查询
count(*) / count(1) 有多少行
count(字段) 有多少行,其中,值是 null 不算
count(distinct 字段) 有多少行,其中,先合并重复的行,值是 null 不算
avg([distinct/all]字段) 计算一列值的平均值(必须是数值类型)
sum([distinct/all]字段) 计算一列值的总和(必须是数值类型)
max([distinct/all]字段) 计算一列值的最大值
min([distinct/all]字段) 计算一列值的最小值
6.分组聚合
-- 分别查询每种难度的题目数量
select difficulty, count(*)
from oj_records
group by difficulty;
在聚合查询时(无论有没有分组),select 后边出现的只能是:
聚合函数
分组凭证
Select question, count(*) from oj_records;
// 只会有 1 行
// question 应该是啥?出现任何的值都不合理,所以不能出现
MySQL 在实现严格模式和非严格模式。非严格模式不报错。
-- 分别查询每个OJ平台的,每种难度的题目数量
select source, difficulty, count(*)
from oj_records
group by difficulty, source
order by source, difficulty;
-- 按照发布时间的年分组聚合
select year(published_at) as y, count(*)
from blogs
group by year(published_at)
order by y;
--在聚合的结果上再次过滤
--where 子句,在聚合之前进行过滤
select year(published_at) as y, count(*)
from blogs
where author = '小红'
group by year(published_at)
order by y;
--having 子句,在聚合之后进行过滤
select year(published_at) as y, count(*) as c
from blogs
group by year(published_at)
having c = 1
order by y;