MySQL 函数

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 后边出现的只能是:

  1. 聚合函数

  2. 分组凭证

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值