mysql--内置函数

目录

这些函数都比较简单,关键在于要灵活使用

目录

1、日期函数

(1)now()

[(2) curdate()](#(2) curdate())

(3)date()

(4)datediff()

(5)adddate()

2、字符串函数

[1. length()](#1. length())

[2. lower()](#2. lower())

[3. upper()](#3. upper())

[4. substring()](#4. substring())

[5. replace()](#5. replace())

3、数学函数

[1. abs()](#1. abs())

[2. ceil()](#2. ceil())

[3. floor()](#3. floor())

[4. round()](#4. round())

[5. sqrt()](#5. sqrt())

4、其他函数

[1. if()](#1. if())

[2. case](#2. case)

[3. coalesce()](#3. coalesce())

[4. concat()](#4. concat())


1、日期函数

函数 说明 语法示例
now() 返回当前日期和时间 select now();
curdate() 返回当前日期 select curdate();
date() 格式化日期 select date('2024-10-15');
datediff() 计算两个日期之间的天数 select datediff('2024-10-15', '2024-10-01');
adddate() 在日期上加上指定的时间 select adddate('2024-10-15', interval 7 day);

(1)now()

返回当前日期和时间。

cpp 复制代码
​select now() as '当前日期和时间';

(2) curdate()

返回当前日期。

cpp 复制代码
select curdate() as '当前日期';

(3)date()

格式化日期。

cpp 复制代码
select date('2001-1-1') as time;

(4)datediff()

计算两个日期之间的天数。

cpp 复制代码
select datediff('2024-10-15', '2024-10-01') AS DaysDifference;

(5)adddate()

在指定日期上加上指定的时间。

cpp 复制代码
select adddate('2024-10-15', interval 7 day) AS NewDate;
cs 复制代码
select adddate('2024-10-15', interval 7 month) AS NewDate;
cpp 复制代码
select adddate('2024-10-15', interval 7 year) AS NewDate;

2、字符串函数

函数 说明 语法示例
length() 返回字符串的长度 select length('hello');
lower() 将字符串转换为小写 select lower('HELLO');
upper() 将字符串转换为大写 select upper('hello');
substring() 返回子字符串 select substring('hello', 1, 3);
replace() 替换字符串中的字符 select replace('hello', 'e', 'a');

1. length()

返回字符串的长度。

cpp 复制代码
select length('hello') AS string_length;

2. lower()

将字符串转换为小写。

cpp 复制代码
select lower('HELLO') AS lower_case_string;

3. upper()

将字符串转换为大写。

cpp 复制代码
select upper('hello') as Uuper_case_string;

4. substring()

返回子字符串。1-3下标的字串

cpp 复制代码
select substring('hello', 1, 3) as substring_result;

5. replace()

替换字符串中的字符。

cpp 复制代码
select replace('hello', 'e', 'a') as replaced_string;

3、数学函数

函数 说明 语法示例
abs() 返回数字的绝对值 select abs(-10);
ceil() 返回大于或等于给定值的最小整数 select ceil(4.2);
floor() 返回小于或等于给定值的最大整数 select floor(4.7);
round() 对数字进行四舍五入 select round(4.5);
sqrt() 返回数字的平方根 select sqrt(16);

1. abs()

返回数字的绝对值。

cpp 复制代码
select abs(-10) as absolute_value;

2. ceil()

返回大于或等于给定值的最小整数。

cpp 复制代码
select ceil(4.2) as ceil_value;

3. floor()

返回小于或等于给定值的最大整数。

cpp 复制代码
select floor(4.7) as floor_value;

4. round()

对数字进行四舍五入。

cpp 复制代码
select round(4.5) as rounded_value;

5. sqrt()

返回数字的平方根。

cpp 复制代码
select sqrt(16) as square_root;

4、其他函数

函数 说明 语法示例
if() 条件判断函数 select if(1 > 0, 'true', 'false');
case 条件表达式 select case when score >= 60 then 'pass' else 'fail' end;
coalesce() 返回第一个非空的值 select coalesce(null, 'default', 'value');
concat() 连接多个字符串 select concat('hello', ' ', 'world');

1. if()

条件判断函数,根据条件返回不同的值。

cpp 复制代码
select if(1 > 0, 'true', 'false') as condition_result;

2. case

条件表达式,根据条件返回不同的结果。

cpp 复制代码
select case when score >= 60 then 'pass' else 'fail' end as result;

3. coalesce()

返回第一个非空的值。

cpp 复制代码
select coalesce(null, 'default', 'value') as first_nonNull_value;

4. concat()

连接多个字符串。

cpp 复制代码
select concat('hello', ' ', 'world') as concatenate__string;
相关推荐
yangchanghua1111 小时前
pgsql 如何查询今天范围内的数据(当天0点0分0秒 - 当天23点59分59秒....)
数据库·pgsql
larance1 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
python_chai1 小时前
从数据汇总到高级分析,SQL 查询进阶实战(下篇)—— 分组、子查询与窗口函数全攻略
数据库·sql·mysql
在努力的前端小白2 小时前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
未来之窗软件服务2 小时前
自建知识库,向量数据库 (九)之 量化前奏分词服务——仙盟创梦IDE
数据库·仙盟创梦ide·东方仙盟·自建ai·ai分词
冒泡的肥皂5 小时前
MVCC初学demo(一
数据库·后端·mysql
.Shu.6 小时前
Redis Reactor 模型详解【基本架构、事件循环机制、结合源码详细追踪读写请求从客户端连接到命令执行的完整流程】
数据库·redis·架构
Bruce_Liuxiaowei8 小时前
MySQL完整重置密码流程(针对 macOS)
mysql
麦麦大数据9 小时前
F003疫情传染病数据可视化vue+flask+mysql
mysql·flask·vue·大屏·传染病
薛晓刚9 小时前
当MySQL的int不够用了
数据库