格式: select fun()[实参列表] [from table_name]; # 当函数用到表中的字段时,带表
特色:web
分类:sql
获取字符串长度svg
select length("hello");
字符串拼接函数函数
select concat(first_name,'_',last_name) AS name from employees;
变大写、变小写spa
select upper("hello"); select lower("HELLO");
获取指定下标后面全部的字符code
获取指定下标下指定长度的字符orm
sql中,index从1开始 select substr("干啥啥不行,吃饭第一名", 7 ); select substr("干啥啥不行,吃饭第一名", 1, 5);
返回子串第一次出现的下标,没有就返回0xml
select instr("hello world", "world");
去除首尾指定字符,若是没有指定,就是去除空格token
select trim(" hello world "); select trim('aa' from "aaaaahello worldaaaaaaaaaa");
左填充,填充指定的字符字符串
右填充,填充指定的字符
select lpad("ok", 10,'*');
字符替换,替换全部
select replace("hello hello world", "hello", "bye");
四舍五入,默认取整数
select round(9.99); select round(9.99, 1);
向上取整
select ceil(1.02); select ceil(-1.02);
向下取证
select floor(1.22); select floor(-1.02);
截断,保留1位小数
select truncate(1.6888,1);
取余
mod(a,b) : a-a/b * b
select mod(10,-3); # 1 select -10 % 3; # -1 select 10 % 3; # 1
获取当前日期+时间
select now();
当前日期
select curdate();
当前时间
select curtime();
获取年
select year(now()); select year('1999-10-18'); select year(hiredate) from employees;
获取月
获取月名(英文)
转换成指定的格式
str_to_date("9-14-1999","%m-%d-%Y") # 用后面的格式解析前面的串到日期类型变量中
日期格式化成字符串
date_format('2019/6/6','%Y年%m月%d日')
Y 1999 y 99 m 01 c 1 d 30 H 24 h 12 i min s 59
select if( a, b, c ) 等效于 if(a) b else c
case 标记 when 值1 then value1/语句1; # 在select中这个分号不要带 when 值2 then value2/语句2; when 值3 then value3/语句3; ... else valuen/语句n; end # 查询员工的工资 根据部门号递增工资的显示 SELECT salary 原始工资, department_id, CASE department_id WHEN 30 THEN salary*1.1 WHEN 40 THEN salary*1.2 WHEN 50 THEN salary*1.3 ELSE salary END AS 新工资 FROM employees;
case when 条件 then value1/语句1 # 无分号 when 条件 then value2/语句2 when 条件 then value3/语句3 ... else valuen/语句n; end
# 用法 select sum(salary), max(salary) from employees;