zoukankan      html  css  js  c++  java
  • Mysql常用函数合集

    1. 字符函数

    • length(获取字节数,UTF-8编码中 一个汉字占3个字节,GBK编码中一个汉字占2个字节)
    select length('abc');
    #结果:3
    
    select length('张三');
    #结果:6
    
    • concat(拼接字符串)
    select concat('a', 'b', 'c');
    #结果:abc
    
    • concat_ws(从二个参数开始拼接字符串,中间以第一个字符隔开)
    select concat_ws(',','a', 'b', 'c');
    #结果:a,b,c
    
    • upper(转换字母为大写)
    select upper('aBc');
    #结果:ABC
    
    • lower(转换字母为小写)
    select lower('AbC');
    #结果:abc
    
    • substr / substring(截取字符串)
    select substr('abcd', 2);
    #结果:bcd
    
    select substr('abcd', 2, 1);
    #结果:b
    
    select substring('abcd', 2);
    #结果:bcd
    
    select substring('abcd', 2, 1);
    #结果:b
    
    • instr(返回子串索引位置,从1开始,没有返回0)
    select instr('abcd', 'bc');
    #结果:2
    
    select instr('abcd', 'e');
    #结果:0
    
    • trim(去除首尾子串,默认去除首尾空格)
    select trim(' abc ');
    #结果:abc
    
    select trim('d' from 'dabcd');
    #结果:abc
    
    • ltrim(去除左侧空格)
    select ltrim(' abc ');
    #结果:abc 
    
    • rtrim(去除右侧空格)
    select rtrim(' abc ');
    #结果: abc
    
    • left(从左边开始第n个字符)
    select left('abc', 1);
    #结果:a
    
    
    • right(从右边开始第n个字符)
    select right('abc', 2);
    #结果:bc
    
    • lpad(左侧补充指定长度字符)
    select lpad('abc', '5', '-');
    #结果:--abc
    
    • rpad(右侧补充指定长度字符)
    select rpad('abc', '5', '-');
    #结果:abc--
    
    • replace(替换字符串)
    select replace('abc', 'b', '-');
    #结果:a-c
    
    • repeat(返回字符串s重复n次后的字符串)
    select repeat('abc', 2);
    #结果:abcabc
    
    • strcmp(比较字符串,相同返回0,左侧大返回1,右侧大返回-1)
    select strcmp('abc', 'abc');
    #结果:0
    
    select strcmp('abcd', 'abc');
    #结果:1
    
    select strcmp('abc', 'abcd');
    #结果:-1
    
    • reverse(反转)
    select reverse('abc');
    #结果:cba
    
    • locate(返回子串在字符串中的其实位置,从1开始)
    select locate('bc', 'abcdef');
    #结果:2
    

    2. 数学函数

    • abs(取绝对值)
    select abs(-1);
    #结果:1
    
    • round(四舍五入,负数时对绝对值四舍五入后加负号)
    select round(1.2);
    #结果:1
    
    select round(-1.2);
    #结果:-1
    
    select round(1.6);
    #结果:2
    
    select round(-1.6);
    #结果:-2
    
    select round(3.1415926, 2);
    #结果:3.14
    
    select round(1234.5678, -2);
    #结果:1200
    
    • ceil(向上取整)
    select ceil(1.2);
    #结果:2
    
    select ceil(-1.2);
    #结果:-1
    
    • floor(向下取整)
    select floor(1.2);
    #结果:1
    
    select floor(-1.2);
    #结果:-2
    
    • rand(随机数,0~1之间)
    select rand();
    #结果:0.3811951860610351
    
    select ceil(rand() * 90 + 10);
    #结果:27
    
    select ceil(rand() * 900 + 100);
    #结果:985
    
    • pi(查询圆周率,默认返回小数点后6位)
    select pi();
    #结果:3.141593
    
    • truncate(截取小数点后位数)
    select truncate('3.1415926', 2);
    #结果:3.14
    
    • mod(取余)
    select mod(10, 3);
    #结果:1
    
    select mod(-10, 3);
    #结果:-1
    
    • pow / power(求次方)
    select pow(2, 3);
    #结果:8
    
    select power(2, 3);
    #结果:8
    
    • greatest(返回集合中最大值)
    select greatest(6, 5, 2, 9, 1);
    #结果:9
    
    • least(返回集合中最小值)
    select least(6, 5, 2, 9, 1);
    #结果:1
    

    3. 日期函数

    • now(当前系统日期时间)
    select now();
    #结果:2020-07-08 19:41:08
    
    • curdate(当前系统日期,不包含时间)
    select curdate();
    #结果:2020-07-08
    
    • curtime(当前系统时间,不包含日期)
    select curtime();
    #结果:19:42:15
    
    • year / month / day / hour / minute / second(返回当前日期时间指定部分)
    select year(now());
    #结果:2020
    
    select month(now());
    #结果:7
    
    select day(now());
    #结果:8
    
    select hour(now());
    #结果:19
    
    select minute(now());
    #结果:43
    
    select second(now());
    #结果:23
    
    • str_to_date(字符串转日期)
    select str_to_date('07/08 2020', '%c/%d %Y');
    #结果:2020-07-08
    
    • date_format(日期转字符串)
    select date_format('2020-07-08', '%Y年%m月%d日');
    #结果:2020年07月08日
    
    • datediff(计算日期只差,单位天)
    select datediff('2020-07-08', '2020-05-08');
    #结果:61
    
    • date_add(日期增加)
    select date_add('2020-07-08 23:59:59', interval 1 second);
    #结果:2020-07-09 00:00:00
    
    select date_add('2020-07-08 23:59:59', interval 1 day);
    #结果:2020-07-09 23:59:59
    
    • date_sub(日期相减)
    select date_sub('2020-07-08 23:59:59', interval 1 second);
    #结果:2020-07-08 23:59:58
    
    select date_sub('2020-07-08 23:59:59', interval 1 day);
    #结果:2020-07-07 23:59:59
    

    4. 条件函数

    • if(判断,类似于三目表达式)
    select if(2 > 1, 'yes', 'no');
    #结果:yes
    
    • ifnull(判断是否为null,否则返回第二个字符)
    select ifnull('abc', 'is null');
    #结果:abc
    
    select ifnull(null, 'is null');
    #结果:is null
    
    • case(多条件判断)
    select
    	case
    when 1 < 2 then
    	'lt'
    when 1 > 2 then
    	'gt'
    else
    	'eq'
    end
    #结果:lt
    

    5. 其他函数

    • version(查询数据库版本)
    select version();
    #结果:8.0.20
    
    • database(查询当前数据库名称)
    select database();
    #结果:testdb
    
    • database(查询当前数据库用户)
    select user();
    #结果:root@127.0.0.1
    
    • connection_id(查询连接数)
    select connection_id();
    #结果:1173
    
    • charset(查询字符集)
    select charset('abc');
    #结果:utf8
    
    • conv(返回数字n为f1进制时的f2进制数)
    select conv(15, 10, 2);
    #结果:1111
    
    select conv(1111, 2, 8);
    #结果:17
    
  • 相关阅读:
    5个最佳WordPress通知栏插件
    最新lombok插件和IDEA2020.1不兼容,Plugin "Lombok" is incompatible (until build 193.SNAPSHOT < IU-201.6668....
    nuxt中localstorage的替代方案
    nuxt或者vue,axios中如何发送多个请求
    wordpress nginx详细环境配置安装命令和相关问题解决
    [no_perms] Private mode enable, only admin can publish this module
    vue bootstrap中modal对话框不显示遮挡打不开
    vue监听当前页面的地址变化/路由变化
    来看看JDK13的81个新特性和API
    Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test java.lang.IllegalStateException
  • 原文地址:https://www.cnblogs.com/cao-lei/p/13272912.html
Copyright © 2011-2022 走看看