MySql数据库查询(DQL)语言—单行函数

版权声明:本博客主要记录学习笔记和遇到的一些问题解决方案,转载请注明出处! https://blog.csdn.net/u010982507/article/details/90734909

简介与分类

单行函数是将一组逻辑语句封装在方法体中,对外暴露方法名。
单行函数分类:

  • 字符函数
  • 数学函数
  • 日期函数
  • 其他函数
  • 流程控制函数

字符函数

  1. length()
    描述:获取长度函数。
    示例:select length('tom');
  2. concat()
    描述:字符连接函数。
    示例:select concat(last_name,first_name) from employees;
  3. upper()
    描述:转大写显示。
    示例:select upper('tom');
  4. lower()
    描述:转小写显示。
    示例:select lower('tom');
    示例:将姓大写,名小写显示
    select concat(upper(last_name), lower(first_name)) from employees;
  5. substr() / substring()
    描述:从某个索引开始截取字符串,索引下标从0开始。
    示例:
    从索引下标6开始截取到末尾。
    select substr('Hello Mysql', 6);
    从索引下标1开始,截取5位。
    select substr('Hello Mysql', 1, 5);
    综合示例:姓名首字母大写,其他小写,然后用_拼接,显示出来。
    select concat(upper(substr(last_name,1,1)), '_', lower(substr(last_name,2))) from employees;
  6. instr()
    描述:返回字串在原串中的第一次出现的起始索引下标,若没有则返回0。
    示例:select instr("Hello World","world") as output; 返回 7
  7. trim()
    描述:去前后空格或指定字符。
    示例:去空格。
    select length(trim(" hello ")) as output;
    示例:去指定字符。
    select trim("a" from "aaaaahelloaaaaa") as output;
  8. lpad() / rpad()
    描述:用指定的字符实现左/右填充指定长度,返回填充后的字符串。
    示例:select lpad("Hello", 10, "*") as output; 返回*****Hello
  9. replace()
    描述:替换指定字符。
    示例:select replace("Hello World","World","MySQL") as output; 输出Hello MySQL
  10. initcap()
    描述:首字母大写。
    示例:select initcap('hello world') 首字符大写 from table;

数学函数

  1. length()
    描述:获取长度函数。
    示例:select length('tom');
  2. round()
    描述:四舍五入。
    示例:select round(1.56); 1.6
    示例:select round(1.568, 2); 1.57 保留两位
  3. ceil()
    描述:向上取整,返回该参数的大于等于该参数的最小整数。
    示例:select ceil(1.56); 2
    示例:select ceil(1.00); 1
    示例:select ceil(-1.02); -1
  4. floor ()
    描述:向下取整,返回小于等于该参数的最大整数。
    示例:select floor(1.56); 1
    示例:select floor(-9.99); -10
  5. truncate ()
    描述:截断,第二个参数表示小数点后保留几位。
    示例:select truncate(1.56, 1); 1.5
  6. mod ()
    描述:取余数,结果与被除数的正负有关,mod(a,b)等价于a-a/b*b
    示例:select mod(10, 3); 1
    示例:select 10%3; 1
    示例:select mod(10, -3); 1
    示例:select mod(-10, 3); -1

日期函数

  1. now()
    描述:返回当前日期+时间。
    示例:select now();
  2. curdate()
    描述:返回当前系统日期,不包含时间。
    示例:select curdate();
  3. curtime()
    描述:返回当前系统时间,不包含日期。
    示例:select curtime();
  4. 获取指定年/月/日/时/分/秒
    示例:select year(now());
    示例:select year('1996-11-19');
    示例:select year(birthday) as 年 from employees;
    示例:select month(now());
    示例:select monthname(now());英文,月
    示例:select day(now());
    示例:select hour(now());
    示例:select minute(now());
    示例:select second(now());
  5. str_to_date()
    描述:将指定字符串格式的时间转换成指定日期的格式。
    示例:select str_to_date('1996-11-19', '%Y-%c-%d');
    示例:select * from employees where hiredate = str_to_date('11-19 1996', '%c-%d %Y');
  6. datediff()
    描述:求日期之间的天数之差。
    示例:select datediff(max(birthday), min(birthday)) from employees;
  7. date_format()
    描述:将日期转换成字符。
    示例:select date_format(now(), "%Y年%m月%d日") as 日期;在这里插入图片描述

其他函数

  1. version()
    描述:查看当前数据库版本号。
    示例:select version();
  2. database()
    描述:查看当前数据库。
    示例:select database();
  3. user()
    描述:查看当前用户。
    示例:select user();

流程控制函数

  1. if()
    描述:相当于if-else效果。
    示例:select if(10>5, '大', '小');
    示例:select last_name, commission_pac, if(commission_pac is null, '没奖金'. '有奖金') as 备注 from employees;
  2. case
    描述一:switch-case效果,适用于等值判断。
    语法:
    case 要判断的字段或者表达式
    when 常量1 then 要显示的(值1)或(表达式1;) 注:表达式要带分号
    when 常量2 then 要显示的(值2)或(表达式2;)
    ......
    else 要显示的(值n)或(表达式n;)
    end
    示例:
    查询员工工资,要求:
    部门号=30,显示原工资的1.1倍;
    部门化=40,显示原工资的1.2倍;
    其他部门,显示原工资。
    select salary as 原工资, department_id,
    case department_id
    when 30 then salary*1.1
    when 40 then salary*1.2
    else salary
    end
    as 新工资 from employees;
    描述二:类似于多重if,适用于不等值判断。
    语法:
    case
    when 常量1 then 要显示的(值1)或(表达式1;) 注:表达式要带分号
    when 常量2 then 要显示的(值2)或(表达式2;)
    ......
    else 要显示的(值n)或(表达式n;)
    end
    示例:
    查询员工工资,要求:
    工资>20000,显示A级别;
    工资>15000,显示B级别;
    工资>10000,显示C级别;
    其他,显示D级别。
    select salary 工资
    case
    when salary >20000 then 'A'
    when salary >15000 then 'B'
    when salary >10000 then 'C'
    else 'D'
    end
    as 工资级别 from employees;

猜你喜欢

转载自blog.csdn.net/u010982507/article/details/90734909
今日推荐