Mysql005—函数

Mysql005—函数,第1张

1.字符串函数:
select concat('hello','mysql');#连接字符串
select lower('HEllo');
select upper('hello');
select lpad('01',5,'234');#左填充
select rpad('01',4,'20');#右填充
select trim(' hello mysql ');#去除头和尾的空格
select substring('hello',1,3);#截取第一到第三

2.数值函数:

/*2.数值函数*/
select ceil(1.1);#向上取整.
select floor(1.9);#向下取整
select mod(3,4);
select rand();#0-1的随机小数
select round(2.345,2);#保留两位小数四舍五入
#生产一个六位的随级数
select  lpad(round(rand()*1000000,0),6,'0');

3.日期函数:

/*3.日期函数*/
select curdate();
select curtime();
select now();

select YEAR(now());
select MONTH(now());
select DAY(now());
select date_add(now(),INTERVAL 70 DAY ); #往后推七十天
select datediff('2021-12-01','2021-10-01');#求取两个日期之间的插值(第一个时间减去第二个时期)

#查询所有员工的入职天数,并根据入职天数倒序排序
select name,datediff(curdate(),entrydate) as 'entrydays' from emp order by entrydays desc;

4.流程函数:

select if(true,'ok','error');#第一个值为true,返回第二个,否则返回第三个
select ifnull('ok','defalut');#判断第一个值是否为null,如果为null返回第二个参数,否则第一个
#查询员工姓名和工作地址
select name,
      ( case workaddress
          when  '江西理工大学信息学院' then '赣州'
          when'江西理工大学机电学院' then '赣州'
          when'东华理工大学化学材料学院' then '南昌'
          when '广西师范大学生命学院' then '广西'
          else  '上海' end)as '大学地点'
from emp;

select id,name,
    (case when age>18 then '成年' else '未成年' end) '年龄'
    from emp;

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/876385.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-13
下一篇2022-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存