常用函数

1.数学函数

 

 SELECT id,is_blacklist,round(is_blacklist,1) as a,
ceil(is_blacklist) as b,
    floor(is_blacklist) as c
from 
user

 

2.字符串函数

 

 select first_name as '名字',length(first_name) as '长度',
upper(first_name) as '大写',lower(first_name) as '小写',
    concat(first_name,last_name) as '拼接',
    left(first_name,2) as '左截取',right(first_name,2) as '右截取',
    substring(first_name,3,2) as '截取'
from 
(
SELECT first_name,last_name FROM school.employees
) t

 

3.日期函数

 

 select month(now()) as '当前月份',
birth_date as '出生日期',
date_add(birth_date,interval 1 day) as '推迟1天',
date_sub(birth_date,interval 1 day) as '提前一天',
    date_format(now(),'%Y-%m') as '今日年月',
    datediff(now(),birth_date) as '距今天数',
    timestampdiff(year,birth_date,now()) as '距今年份',
    timestampdiff(day,birth_date,now()) as '距今天数'
from
employees

 

4.条件函数

 

select id,like_me,
ifnull(like_me,10) as '空令10代替',
if(id%2=0,1,0) as '奇0偶1',
    (case when id%2=0 then 0 else 1 end) as '偶0奇1' 
from
test

 

5.统计函数

 

 select id,
count(id) as '计数',
    sum(id) as '和',
    round(avg(id),2) as '均值',
    min(id) as '最小',
    max(id) as '最大'
from
test

 

posted @ 2022-08-05 18:14  萧六弟  阅读(57)  评论(0)    收藏  举报