MySQL(十一):MySQL语法-函数

原始数据

select * from employees where emp_no = '10002';
select * from salaries where emp_no = '10002';
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date  |
+--------+------------+------------+-----------+--------+------------+
|  10002 | 1964-06-02 | Bezalel    | Simmel    | F      | 1985-11-21 |
+--------+------------+------------+-----------+--------+------------+
1 row in set (0.00 sec)

+--------+--------+------------+------------+
| emp_no | salary | from_date  | to_date    |
+--------+--------+------------+------------+
|  10002 |  65828 | 1996-08-03 | 1997-08-03 |
|  10002 |  65909 | 1997-08-03 | 1998-08-03 |
|  10002 |  67534 | 1998-08-03 | 1999-08-03 |
|  10002 |  69366 | 1999-08-03 | 2000-08-02 |
|  10002 |  71963 | 2000-08-02 | 2001-08-02 |
|  10002 |  72527 | 2001-08-02 | 9999-01-01 |
+--------+--------+------------+------------+
6 rows in set (0.00 sec)

函数

在这里插入图片描述

select
    now()                                              as '查询时间',

    a.emp_no                                           as '员工编号',

    concat(b.last_name, ' ', b.first_name)             as '姓名',
    concat_ws(':', b.last_name, b.first_name)          as '姓名(字符串拼接)',

    replace(b.last_name, 'Simmel', 'Simmel-齐美尔')     as '姓(替换)',
    ucase(b.last_name)                                 as '姓(大写)',
    upper(b.last_name)                                 as '姓(大写)',
    mid(b.last_name, 1, 1)                             as '姓(首字母-从索引1开始计数)',

    replace(b.first_name, 'Bezalel', '贝扎雷-Bezalel')  as '名(替换)',
    lcase(b.first_name)                                as '名(小写)',
    lower(b.first_name)                                as '名(小写)',
    length(b.first_name)                               as '名(长度)',
    field('Bezalel', b.last_name, b.first_name)        as 'Bezalel(名的索引)',

    trim('     做个人吧     ')                          as '去除前后空格',

    b.gender                                           as '性别',

    repeat(b.gender, 10)                               as '复制10分性别',

    if(b.gender = 'F', '女', '男')                      as '性别(三元运算)',

    case
        when b.gender = 'F' then '女'
        when b.gender = 'M' then '男'
        else '变态' end
                                                       as '性别(三元运算)',

    isnull(b.gender)                                   as '是否为变态(isnull)',
    isnull(null)                                       as '是否为null(isnull)',

    ifnull(b.gender, '变态')                            as '是否为变态(ifnull)',
    ifnull(null, 'null')                               as '是否为null(ifnull)',

    coalesce(b.gender, '变态')                          as '是否为变态(coalesce)',
    coalesce(null, 'null')                             as '是否为null(coalesce)',

    b.birth_date                                       as '出生日期',
    b.hire_date                                        as '雇佣日期',

    left(b.birth_date, 4)                              as '年(从左侧截取4个字符)',
    substring(b.birth_date, 6, 2)                      as '月(从左侧6处开始截取2个字符)',
    substr(b.birth_date, 6, 2)                         as '月(从左侧6处开始截取2个字符)',
    right(b.birth_date, 2)                             as '日(从右侧截取2个字符)',

    substring_index('www.baidu.com', '.', -2)          as '以.分割字符串逆向截取两个元素',
    locate('.', 'baidu.com.cn')                        as '.第一次出现的位置',

    group_concat(a.salary separator ';')               as '薪资',
    count(a.salary)                                    as '数量',

    avg(a.salary)                                      as '平均值',
    round(avg(a.salary))                               as '四舍五入(平均值)',
    format(avg(a.salary), 0)                           as '平均值(格式化)',
    format(avg(a.salary), 10)                          as '平均值(格式化)',
    sum(a.salary)                                      as '总和',
    max(a.salary)                                      as '最大值',
    min(a.salary)                                      as '最小值',

    sqrt(sum(a.salary))                                as '总和平方根',

    rand() * 100                                       as '随机数(0到1 * 100)',

    floor(1.2345)                                      as '返回不大于参数的最大整数',
    floor(-1.2345)                                     as '返回不大于参数的最大整数',

    ceiling(1.2345)                                    as '返回不小于参数的最小整数',
    ceiling(-1.2345)                                   as '返回不小于参数的最小整数',

    abs(-2)                                            as '绝对值',

    5 div 2                                            as '5/2',

    mod(3, 2)                                          as '3%2',
    mod(2, 3)                                          as '2%3',

    pow(2, 3)                                          as '2^3',
    power(2, 3)                                        as '2^3',

    pi()                                               as '圆周率',

    cos(pi())                                          as 'cos(余弦)',
    sin(pi())                                          as 'sin(正弦)',
    tan(pi())                                          as 'tan(正切)',

    acos(1)                                            as 'acos(反余弦)',
    asin(1)                                            as 'asin(反正弦)',
    atan(1)                                            as 'atan(反正切)',

    least(2, 1, 4, 0, 1)                               as '返回最小值',
    greatest(2, 1, 4, 0, 1)                            as '返回最大值',

    degrees(pi())                                      as '从弧度转换为角度',
    radians(180)                                       as '从角度转换为弧度',

    truncate(0.123456789, 5)                           as '保留有效数字(不四舍五入)',

    sign(-100)                                         as '参数符号',
    sign(0)                                            as '参数符号',
    sign(100)                                          as '参数符号'
from
    (select * from salaries where emp_no = '10002') as a
inner join
    employees as b
on
    a.emp_no = b.emp_no
group by
    a.emp_no;

日期函数

在这里插入图片描述

select date_add('2023-01-01', interval 7 day)                              as '增加7天',
       adddate('2023-01-01', interval 7 hour)                              as '增加7小时',
       adddate('2023-01-01', interval 7 minute)                            as '增加7分钟',
       adddate('2023-01-01', interval '07-01' year_month)                  as '增加7年1月',

       date_sub('2023-01-20', interval 7 day)                              as '减去7天',
       subdate('2023-01-20', interval 7 hour)                              as '减去7小时',

       addtime('00:00:00', '01:00:00')                                     as '增加1小时',
       subtime('00:00:00', '07:07:07')                                     as '减去7小时7分7秒',

       convert_tz('2023-01-01 01:01:01', '+8:00', '+10:00')                as '从+8:00时区修改为+9:00时区',

       curdate()                                                           as '返回当前日期',
       current_date()                                                      as '返回当前日期',
       current_date                                                        as '返回当前日期',

       curtime()                                                           as '返回当前时间',
       current_time()                                                      as '返回当前时间',
       current_time                                                        as '返回当前时间',

       now()                                                               as '返回当前日期时间',
       localtime()                                                         as '返回当前日期时间',
       localtime                                                           as '返回当前日期时间',
       localtimestamp()                                                    as '返回当前日期时间',
       localtimestamp                                                      as '返回当前日期时间',
       current_timestamp()                                                 as '返回当前日期时间',
       current_timestamp                                                   as '返回当前日期时间',

       date_format(now(), '%Y(4位年) - %y(2位年)')                         as '年',

       date(now())                                                         as '从 date 或者 datetime 表达式中提取出日期部分',

       datediff('2023-01-01 10:10:10', '1997-02-11 07:01:01')              as '日期相减得出天',

       day('2020-01-31')                                                   as '返回某天是当月的第几天(1-31)',
       dayofmonth('2020-02-31')                                            as '返回某天是当月的第几天(1-31)',

       dayname('2020-01-31')                                               as '返回某天在用星期中的名称',

       dayofweek('2020-01-31')                                             as '返回某天是该星期的第几天',

       dayofyear('2020-02-21')                                             as '返回某天是一年中的第几天(1-366)',

       extract(year_month from now())                                      as '提取日期中的某一部分',

       from_days(366)                                                      as '将天数转换为日期',

       from_unixtime(1580436610)                                           as '将某个 UNIX 时间戳格式化为日期',

       unix_timestamp('2020-01-31 10:10:10')                               as '将某个日期格式化为 UNIX 时间戳',

       last_day('2020-01-10 10:10:10')                                     as '返回参数日期所在月份的最后一天',

       makedate(2023, 100)                                                 as '利用年份和某天在该年所处的天数来创建日期',

       maketime(112, 50, 50)                                               as '根据小时,分钟和秒值创建并返回时间值',

       year(now())                                                         as '由参数返回年',
       month(now())                                                        as '由参数返回月份',
       monthname(now())                                                    as '由参数返回月份名称',
       yearweek(now())                                                     as '由参数返回年周数',
       weekofyear(now())                                                   as '由参数返回周数',
       weekday(now()) + 1                                                  as '返回星期几',
       week(now())                                                         as '由参数返回周数',
       time(now())                                                         as '时间',
       hour(now())                                                         as '由参数返回小时',
       minute(now())                                                       as '由参数返回分钟',
       second(now())                                                       as '由参数返回秒',
       microsecond(now())                                                  as '由参数返回毫秒',

       to_days(now())                                                      as '将日期参数转换为天数',

       utc_date()                                                          as '返回当前 UTC 日期',
       utc_time()                                                          as '返回当前 UTC 时间',
       utc_timestamp()                                                     as '返回当前 UTC 日期和时间',

       quarter(now())                                                      as '返回日期参数所在的季度',

       sec_to_time(100000)                                                 as '将秒数转换为 ''HH:MM:SS'' 格式',


       str_to_date('2020-01-10 10:20:30', '%Y-%m-%d %H:%i:%s')             as '将字符串转换为日期数据',

       sysdate()                                                           as '返回函数执行的时的时刻',

       time_format('20201010 101010', '%Y-%m-%d %H:%i:%s')                 as '格式化时间',

       time_to_sec('10:10:10')                                             as '将时间参数转换为秒数',

       timediff('2020-01-01 10:10:10', '2023-01-01 20:20:20')              as '将两个时间相减返回时分秒',

       period_add(201808, 2)                                               as '参数年月加上参数月',
       period_diff(201808, 201810)                                         as '返回两个年月格式的日期数据之间的月份数',

       timestampadd(day, 3, now())                                         as '3天以后的日期时间',
       timestampdiff(second, '2023-07-21 10:10:10', '2023-07-20 01:01:01') as '两个日期时间相减',

       timestamp('2020-01-01')                                             as '返回参数的日期时间',

       timestamp(now(), '3:00:00')                                        as '返回参数日期时间加上时间';

猜你喜欢

转载自blog.csdn.net/weixin_43526371/article/details/131788871