概述
- 在
MySQL
中,单行处理函数用于对查询结果中的每一行数据进行处理
字符串函数
concat
- 将多个字符串连接成一个字符串
1 |
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees; |
substr
- 取子串
- (被截取的字符串,起始下标,截取的长度)
1 2 3 4 |
//截取ename这一列的首字母(下标是从1开始) select substr(ename,1,1) from emp; SELECT SUBSTRING(emp_name, 1, 3) AS short_name FROM employees; |
length
- 返回字符串的长度
1 2 3 |
select length(ename) from emp; SELECT LENGTH(emp_name) AS name_length FROM employees; |
lower
- 将字符串转换为小写
1 2 |
//把name这一行的值转换成小写 select lower(name) from emp; |
upper
- 将字符串转换为大写
1 |
select upper(name) from emp; |
trim
- 去掉字符串两端的空格
1 2 |
//去除字符串前后两边的空格 select ename from emp where ename = trim(' from hell '); |
replace
- 替换字符串中的子字符串
1 |
SELECT REPLACE(emp_name, 'a', 'o') AS replaced_name FROM employees; |
str_to_date
- 用于将字符串转换为日期或时间类型
1 2 |
SELECT order_id, STR_TO_DATE(order_date, '%m-%d-%Y') AS order_date_formatted FROM orders; |
格式符 | 功能 |
%Y | 四位的年份 |
%y | 两位的年份 |
%m | 月份(01,02,...,12) |
%c | 月份(1,2,...,12) |
%d | 日 |
%H | 24小时制 |
%h | 12小时制 |
%i | 分钟(00,01,...,59) |
%S或者%s | 秒(00,01,...,59) |
1 2 3 4 5 6 |
//这个字符串mysql会自动转换 select ename,hiredate from emp where hiredate = '1981-12-03'; //下面这个mysql就不识别了,因为mysql默认格式是年月日 select ename,hiredate from emp where hiredate = '03-12-1981'; //所以我们需要对它进行转换 select ename,hiredate from emp where hiredate = str_to_date('03-12-1981','%d-%m-%Y'); |
数值函数
abs
- 返回数值的绝对值
1 |
SELECT ABS(salary - 5000) AS salary_difference FROM employees; |
round
- 对数值进行四舍五入
1 2 3 4 5 6 7 8 |
SELECT ROUND(salary, 2) AS rounded_salary FROM employees; //四舍五入到个位-123 select round(123); //四舍五入小数点后1位-124 select round(123.54,0); //四舍五入小数点后2位-123.5 select round(123.54,1); |
ceil
- 返回大于或等于指定数值的最小整数
1 |
SELECT CEIL(salary) AS ceiling_salary FROM employees; |
floor
- 返回小于或等于指定数值的最大整数
1 |
SELECT FLOOR(salary) AS floor_salary FROM employees; |
mod
- 返回两个数相除的余数
1 |
SELECT MOD(salary, 1000) AS salary_mod FROM employees; |
power
- 返回数值的指定次方
1 |
SELECT POWER(salary, 2) AS salary_squared FROM employees; |
rand
- 生成随机数-(0到1直接的闭区间)
1 2 3 4 |
//[0,1]之间的随机数 select rand(); //[0,100]直接的随机数 select round(rand()*100); |
format
- 用于格式化数值,通常用于将数值格式化为特定的小数位数和添加千位分隔符
1 2 |
SELECT sale_id, FORMAT(amount, 2) AS formatted_amount FROM sales; |
ifnull
- 用于在表达式为 NULL 时返回指定的替代值
1 2 |
SELECT emp_id, emp_name, IFNULL(commission, 0) AS commission FROM employees; |
日期和时间函数
now
- 返回当前日期和时间
1 |
SELECT NOW() AS current_datetime; |
curdate
- 返回当前日期
1 |
SELECT CURDATE() AS current_date; |
curtime
- 返回当前时间
1 |
SELECT CURTIME() AS current_time; |
date
- 从日期时间值中提取日期部分
1 |
SELECT DATE(hire_date) AS hire_date_only FROM employees; |
year
- 从日期时间值中提取年份
1 |
SELECT YEAR(hire_date) AS hire_year FROM employees; |
month
- 从日期时间值中提取月份
1 |
SELECT MONTH(hire_date) AS hire_month FROM employees; |
day
- 从日期时间值中提取日期
1 |
SELECT DAY(hire_date) AS hire_day FROM employees; |
date_format
- 按照指定格式格式化日期
1 |
SELECT DATE_FORMAT(hire_date, '%Y-%m-%d') AS formatted_hire_date FROM employees; |
其他函数
if
- 返回条件表达式的结果
1 |
SELECT emp_name, IF(salary > 5000, 'High', 'Low') AS salary_level FROM employees; |
coalesce
- 返回第一个非空值
1 |
SELECT COALESCE(middle_name, 'N/A') AS middle_name FROM employees; |
case
- 基于条件表达式返回不同的结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
SELECT emp_name, CASE WHEN salary < 3000 THEN 'Low' WHEN salary BETWEEN 3000 AND 6000 THEN 'Medium' ELSE 'High' END AS salary_grade FROM employees; select ename,job,(case job when 'MANAGER' then sal*1.1 when 'SALESMAN' then sal*1.5 else sal end ) as newsal from emp; |
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 表_约束-主键10/26
- ♥ 表操作_数据排序10/23
- ♥ 视图10/28
- ♥ 数据库设计三范式简介10/28
- ♥ 连接查询-笛卡尔积 || 连接方式10/25
- ♥ 数据处理函数:多行处理函数10/24