MYSQL函数

MYSQL函数

– 数学函数

abs,ceil,floor,greatest,least,max,min,等等
-- 取绝对值
select abs(-19); -- 19
-- 向上取整
select ceil(1.1); -- 2
-- 向下取整
select floor(1.1); -- 1
-- 列表最大值
select greatest(1,2,3,4,0);
-- 列表最小值
select least(1,2,3,4,0);
-- 求余数(取模)
select mod(5,2); -- 1
-- pi() 返回圆周率
select pi(); -- 3.141593
-- 求X的Y次方 pow(x,y)
select pow(3,2);  -- 9;
-- 取随机数
select rand();
select floor(rand()*100);
-- 取四舍五入
select round(pi(),3); -- 3.142
-- 直接截取小数
select truncate(pi(),3); -- 3.141

– 字符串函数

-- char_length
select char_length('hello');  -- 5
select char_length('你好啊');  -- 3
select char_length(',');  -- 1
select char_length(' ');  -- 1
-- length 返回字节  UTF-8编码、
select length('hello'); -- 5
select length('你好啊'); -- 9
-- 字符串合并
select concat('hello','world'); -- helloworld
-- 指定分隔符并合并字符串
select concat_ws('-','hello','world'); -- hello-world

-- 返回字符串再来列表中第一次出现的位置
select field('a','b','a','d');  -- 2
-- 去掉空格
select ltrim('  abc  '); -- 去掉左边
select trim('  abc  '); -- 去掉全部
select rtrim('  a bc  '); -- 去掉右边
-- 截取字符串
select mid('helloworld',2,3);
-- 获取位置
select position('abc'in'helloabcworld');
-- 替换
select replace('helloaaaworld','aa','bbb');
-- 字符串翻转
select reverse('hello');
-- 返回字符串的后几个字符
select right('hello',3); -- llo
-- 字符串比较  一样返回0,否则比较大小,按字典顺序
select  strcmp('hello','world'); -- -1
-- 字符串截取
select substr('helloworld',2,3); -- ell
-- 字符串小写大写转换
select ucase('hello'); -- HELLO
select upper('hello'); -- HELLO
select lcase('HELLO'); -- hello
select lower('HELLO'); -- hello

– 日期函数

-- 获取时间戳(毫秒值)
select unix_timestamp();
-- 将日期格式转为时间戳
select unix_timestamp('2023-01-01 08:08:08');
-- 将时间戳转换成指定格式的日期
select from_unixtime(1672531688,'%Y年%m月%d %h:%i:%s');
-- 得到当前年月日
select curdate();
select current_date();
-- 获取当前时分秒
select curtime();
select current_time();
-- 获取完整时间
select current_timestamp();
-- 从日期字符串中获取年月日
select date('2023-12-12 08:34:32');
-- 算出日期差
select datediff(curdate(),'2000-12-12');
-- 算出时间差
select timediff(curtime(),'12:11:56');
-- 日期格式化
select date_format('1200-12-2,9:9:9','%Y-%m-%d %H-%i-%s');
-- 字符串转换成日期 对准格式
select str_to_date('2002-1-23 5:5:5 ','%Y-%m-%d %H:%i:%s');
-- 日期运算
select date_sub('2023-02-12',interval 2 day); -- 向后跳转
select date_add('2023-02-12',interval 2 month); -- 向前跳转  或adddate
--  从日期中获取时间
select extract(hour from '1200-12-02 09:09:09');
select extract(year from '1200-12-02 09:09:09');
-- 获取给定日期的月的最后一天
select last_day('2023-12-23');
-- 获取所给年份的指定天数的日期
select makedate('2023',32);
-- 从所给日期中获得时间
select year('2023-12-23 03:34:34');
select month('2023-12-23 03:34:34');
select day('2023-12-23 03:34:34');
select minute('2023-12-23 03:34:34');
select quarter('2023-12-23 03:34:34'); -- 获取季度
select monthname('2023-12-23 03:34:34'); -- 获取月份名字
select dayname('2023-12-23 03:34:34');  -- 获取周几
select dayofyear('2023-12-23 03:34:34'); -- 获取当年的第几天
select week('2023-12-23 03:34:34'); -- 获取时间是当年的第几周 51
select weekday('2023-12-23 03:34:34'); -- 返回周几 5表示周六
select weekofyear('2023-12-23 03:34:34'); -- 返回第几周 51
select yearweek('2023-12-23 03:34:34'); -- 返回年份及第几周 202351
select now(); -- 返回当前时间


– 控制流函数

-- if逻辑判断语句
-- if(expr,v1,v2);第一个是表达式 正确取第二个,否则取第三个
select if(1>0,'你是***','真聪明');
-- ifnull(vi,v2) 如果vi的值不为null,则返回vi,反之则返回v2
select ifnull(null,'零');
-- isnull
select isnull(4); -- 0
select isnull(null); -- 1
-- nullif(v1,v2); v1等于v2,返回null,否则返回v1

select nullif(12,13); -- 12

-- case when 语句

select 
	case 5
		when 1 then '错误'
		when 2 then 'hello'
		when 5 then '正确'
		else '其他'
		end as info;


– 窗口函数

-- 序号函数 分布函数 前后函数 头尾函数 其他函数
/* 格式
	window_function expr over(
		partition by
		order by
		frame_clause
);
		*/

-- 序号函数 row_number() rank() dense_rank()
-- 开窗聚合函数 max() min avg sum count 
-- 分布函数 cume_dist() precent_rank()
-- 前后函数 lag lead
-- 头尾函数 first_value last_value
-- 其他函数  nth_value ntile

具体窗口函数请移步其他博主

相关推荐

  1. MySQL函数

    2024-02-03 20:50:01       69 阅读
  2. MYSQL函数

    2024-02-03 20:50:01       44 阅读
  3. Mysql函数

    2024-02-03 20:50:01       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-03 20:50:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-03 20:50:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-03 20:50:01       82 阅读
  4. Python语言-面向对象

    2024-02-03 20:50:01       91 阅读

热门阅读

  1. Elastic Search

    2024-02-03 20:50:01       56 阅读
  2. 开源软件的影响力

    2024-02-03 20:50:01       49 阅读
  3. 【MySQL】MySQL 查询两个日期内的每一天

    2024-02-03 20:50:01       48 阅读
  4. 在nodejs中使用mysql2

    2024-02-03 20:50:01       48 阅读
  5. themeleaf:入门(一)

    2024-02-03 20:50:01       44 阅读
  6. kotlin 多字段去重

    2024-02-03 20:50:01       42 阅读
  7. 【React】React预览docx文件

    2024-02-03 20:50:01       49 阅读
  8. UDP和TCP的区别和联系

    2024-02-03 20:50:01       45 阅读
  9. 【CSS】动画(Transform,Transition,Animation)

    2024-02-03 20:50:01       48 阅读