【MySQL】MySQL内置函数--日期函数/字符串函数/数学函数/其他相干函数 ...

打印 上一主题 下一主题

主题 1908|帖子 1908|积分 5724

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.日期函数

MySQL中内置了一下函数:
函数名称形貌current_date()当前日期current_time()当前时间current_timestamp()当前时间戳date(datetime)返回datetime参数的日期部分date_add(date,interval d_value_type)在date中添加日期或时间,interval后的数值单元可以是year minute second daydate_sub(date,interval d_value_type)在date中减去日期或时间,interval后的数值单元可以是year minute second daydatediff(date1,date2)两个日期的差,单元是天now()当前日期时间 得到年月日
  1. mysql> select current_date();
  2. +----------------+
  3. | current_date() |
  4. +----------------+
  5. | 2023-11-11     |
  6. +----------------+
  7. 1 row in set (0.00 sec)
复制代码
得到时分秒
  1. mysql> select current_time();
  2. +----------------+
  3. | current_time() |
  4. +----------------+
  5. | 15:46:47       |
  6. +----------------+
  7. 1 row in set (0.00 sec)
复制代码
得到时间戳
  1. mysql> select current_timestamp();
  2. +---------------------+
  3. | current_timestamp() |
  4. +---------------------+
  5. | 2023-11-11 15:47:19 |
  6. +---------------------+
  7. 1 row in set (0.00 sec)
复制代码
在日期的底子上加日期
  1. mysql> select date_add('2023-11-11',interval 10 day);
  2. +----------------------------------------+
  3. | date_add('2023-11-11',interval 10 day) |
  4. +----------------------------------------+
  5. | 2023-11-21                             |
  6. +----------------------------------------+
  7. 1 row in set (0.01 sec)
复制代码
在日期的底子上减去时间
  1. mysql> select date_sub('2023-11-11',interval 10 day);
  2. +----------------------------------------+
  3. | date_sub('2023-11-11',interval 10 day) |
  4. +----------------------------------------+
  5. | 2023-11-01                             |
  6. +----------------------------------------+
  7. 1 row in set (0.00 sec)
复制代码
计算两个日期之间相差多少天
  1. mysql> select datediff('2023-11-11','2023-10-21');
  2. +-------------------------------------+
  3. | datediff('2023-11-11','2023-10-21') |
  4. +-------------------------------------+
  5. |                                  21 |
  6. +-------------------------------------+
  7. 1 row in set (0.01 sec)
复制代码
创建一个留言表
  1. mysql> create table messsage(
  2.     -> id int primary key auto_increment,
  3.     -> content varchar(32) not null,
  4.     -> sendtime datetime
  5.     -> );
  6. Query OK, 0 rows affected (0.03 sec)
复制代码
插入数据
  1. mysql> insert into message(content,sendtime) values('hello world',now());
  2. mysql> insert into message(content,sendtime) values('hello world',now());
  3. mysql> select * from message;
  4. +----+-------------+---------------------+
  5. | id | content     | sendtime            |
  6. +----+-------------+---------------------+
  7. |  3 | hello world | 2023-11-11 16:03:38 |
  8. |  4 | hello world | 2023-11-11 16:03:48 |
  9. +----+-------------+---------------------+
  10. 2 rows in set (0.00 sec)
复制代码
请查询在2分钟内发布的帖子
  1. mysql> select * from message where date_add(sendtime ,interval 2 minute) > now();
  2. +----+-------------+---------------------+
  3. | id | content     | sendtime            |
  4. +----+-------------+---------------------+
  5. |  3 | hello world | 2023-11-11 16:03:38 |
  6. |  4 | hello world | 2023-11-11 16:03:48 |
  7. +----+-------------+---------------------+
  8. 2 rows in set (0.00 sec)
  9. ------------------------------|-----------|-------------|------------------
  10.                               初始时间     now()       初始时间+2min
复制代码
2.字符串函数

函数名称形貌charset(str)返回字符串字符集concat(string2 [,…])毗连字符串instr(string,substring)返回substring在string中出现的位置,没有返回0ucase(string2)转换成大写lcase(string2)转换成小写left(string2,length)从string2中的左边取length个字符right(string2,length)从string2中的右边取length个字符length(string)string的长度replace(str, search_str, replace_str)在str中用replace_str 替换search_strstrcmp(string1, string2)逐字符比力两个字符串巨细substring(str, position [,length])从str的postion开始,取length个字符ltrim(string) rtrim(string) trim(string)去除前空格或后空格 获取emp表的ename列的字符集
  1. select charset(ename) from emp;
复制代码
要求显示exam_result表中的信息,显示格式:“XXX的语文是XXX分,数学XXX分,英语XXX分”
  1. select concat(name, '的语文是',chinese,'分,数学是',math,'分') as '分数' from student;
复制代码
求学生表中学生姓名占用的字节数
  1. select length(name), name from student;
复制代码
留意:length函数返回字符串长度,以字节为单元。如果是多字节字符则计算多个字节数;如果是单字节字符则算作一个字节。好比:字母,数字算作一个字节,中文表示多个字节数(与字符集编码有关)
将emp表中全部名字中有S的替换成’上海’
  1. select replace(ename, 'S', '上海') ,ename from emp;
复制代码
截取emp表中ename字段的第二个到第三个字符
  1. select substring(ename, 2, 2), ename from emp;
复制代码
以首字母小写的方式显示全部员工的姓名
  1. select concat(lcase(substring(ename,1,1)),substring(ename,2)) from emp;
复制代码
3.数学函数

函数名称形貌abs(number)绝对值bin(decimal_number)十进制转换二进制hex(decimalNumber)转换成十六进制conv(number,from_base,to_base)进制转换ceiling(number)向上取整floor(number)向下取整format(number,decimal_places)格式化,保存小数位数rand()返回随机浮点数,范围[0.0,1.0)mod(number,denominator)取模,求余 绝对值
  1. select abs(-100.2);
  2. mysql> select abs(-100.2);
  3. +-------------+
  4. | abs(-100.2) |
  5. +-------------+
  6. |       100.2 |
  7. +-------------+
  8. 1 row in set (0.00 sec)
复制代码
向上取整
  1. mysql> select ceiling(3.1);
  2. +--------------+
  3. | ceiling(3.1) |
  4. +--------------+
  5. |            4 |
  6. +--------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select ceiling(3.9);
  9. +--------------+
  10. | ceiling(3.9) |
  11. +--------------+
  12. |            4 |
  13. +--------------+
  14. 1 row in set (0.00 sec)
  15. mysql> select ceiling(-3.1);
  16. +---------------+
  17. | ceiling(-3.1) |
  18. +---------------+
  19. |            -3 |
  20. +---------------+
  21. 1 row in set (0.00 sec)
  22. mysql> select ceiling(-3.9);
  23. +---------------+
  24. | ceiling(-3.9) |
  25. +---------------+
  26. |            -3 |
  27. +---------------+
  28. 1 row in set (0.00 sec
复制代码
向下取整
  1. mysql> select floor(3.1);
  2. +------------+
  3. | floor(3.1) |
  4. +------------+
  5. |          3 |
  6. +------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select floor(3.9);
  9. +------------+
  10. | floor(3.9) |
  11. +------------+
  12. |          3 |
  13. +------------+
  14. 1 row in set (0.00 sec)
  15. mysql> select floor(-3.1);
  16. +-------------+
  17. | floor(-3.1) |
  18. +-------------+
  19. |          -4 |
  20. +-------------+
  21. 1 row in set (0.00 sec)
  22. mysql> select floor(-3.9);
  23. +-------------+
  24. | floor(-3.9) |
  25. +-------------+
  26. |          -4 |
  27. +-------------+
  28. 1 row in set (0.00 sec)
复制代码
保存2位小数位数(小数四舍五入)
  1. mysql> select format(12.345,2);
  2. +------------------+
  3. | format(12.345,2) |
  4. +------------------+
  5. | 12.35            |
  6. +------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select format(99.99,2);
  9. +-----------------+
  10. | format(99.99,2) |
  11. +-----------------+
  12. | 99.99           |
  13. +-----------------+
  14. 1 row in set (0.00 sec)
  15. mysql> select format(99.999,2);
  16. +------------------+
  17. | format(99.999,2) |
  18. +------------------+
  19. | 100.00           |
  20. +------------------+
  21. 1 row in set (0.00 sec)
  22. mysql> select format(-12.223,2);
  23. +-------------------+
  24. | format(-12.223,2) |
  25. +-------------------+
  26. | -12.22            |
  27. +-------------------+
  28. 1 row in set (0.00 sec)
  29. mysql> select format(-12.227,2);
  30. +-------------------+
  31. | format(-12.227,2) |
  32. +-------------------+
  33. | -12.23            |
  34. +-------------------+
  35. 1 row in set (0.00 sec)
复制代码
产生随机数
  1. mysql> select rand();
  2. +--------------------+
  3. | rand()             |
  4. +--------------------+
  5. | 0.5963675860281732 |
  6. +--------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select rand()*10;
  9. +--------------------+
  10. | rand()*10          |
  11. +--------------------+
  12. | 3.1565381243420187 |
  13. +--------------------+
  14. 1 row in set (0.00 sec)
复制代码
4.其它函数



  • user() 查询当前用户
  1. select user();
  2. mysql> select user();
  3. +----------------+
  4. | user()         |
  5. +----------------+
  6. | root@localhost |
  7. +----------------+
  8. 1 row in set (0.00 sec)
复制代码


  • database()显示当前正在使用的数据库
  1. select database();
  2. mysql> select database();
  3. +------------+
  4. | database() |
  5. +------------+
  6. | scott      |
  7. +------------+
  8. 1 row in set (0.00 sec)
复制代码


  • md5(str)对一个字符串举行md5摘要,摘要后得到一个32位字符串
  1. select md5('admin');
  2. mysql> select md5('admin');
  3. +----------------------------------+
  4. | md5('admin')                     |
  5. +----------------------------------+
  6. | 21232f297a57a5a743894a0e4a801fc3 |
  7. +----------------------------------+
  8. 1 row in set (0.00 sec)
复制代码


  • password()函数,MySQL数据库使用该函数对用户加密
  1. select password('root');
  2. mysql> select password('root');
  3. +-------------------------------------------+
  4. | password('root')                          |
  5. +-------------------------------------------+
  6. | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
  7. +-------------------------------------------+
  8. 1 row in set, 1 warning (0.01 sec)
复制代码


  • ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值
  1. mysql> select ifnull('abc',null);
  2. +--------------------+
  3. | ifnull('abc',null) |
  4. +--------------------+
  5. | abc                |
  6. +--------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select ifnull(null, '123');
  9. +---------------------+
  10. | ifnull(null, '123') |
  11. +---------------------+
  12. | 123                 |
  13. +---------------------+
  14. 1 row in set (0.00 sec)
  15. mysql> select ifnull('abc', '123');
  16. +----------------------+
  17. | ifnull('abc', '123') |
  18. +----------------------+
  19. | abc                  |
  20. +----------------------+
  21. 1 row in set (0.00 sec)
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

花瓣小跑

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表