MySQL函数
发布时间 :
字数:489
阅读 :
概述
MySQL函数是MySQL数据库中的一种特殊的语法,用于对数据进行处理和计算,本文介绍了MySQL函数的基本语法和常用操作。
文本处理
函数 |
说明 |
示例 |
UPPER |
将字符串转换为大写 |
SELECT UPPER('hello') AS Uppercase; |
LOWER |
将字符串转换为小写 |
SELECT LOWER('WORLD') AS Lowercase; |
LENGTH |
获取字符串的长度 |
SELECT LENGTH('Moonshot') AS Length; |
CONCAT |
拼接字符串 |
SELECT CONCAT('Hello', ' ', 'World') AS ConcatenatedString; |
SUBSTRING |
截取字符串 |
SELECT SUBSTRING('Moonshot', 1, 5) AS Substring; |
REPLACE |
替换字符串 |
SELECT REPLACE('Moonshot', 'Moon', 'Star') AS ReplacedString; |
日期和时间处理
函数 |
说明 |
示例 |
NOW |
获取当前日期和时间 |
SELECT NOW() AS CurrentDateTime; |
CURDATE |
获取当前日期 |
SELECT CURDATE() AS CurrentDate; |
CURTIME |
获取当前时间 |
SELECT CURTIME() AS CurrentTime; |
DATE |
获取日期部分 |
SELECT DATE('2024-09-18 08:23:45') AS DatePart; |
TIME |
获取时间部分 |
SELECT TIME('2024-09-18 08:23:45') AS TimePart; |
YEAR |
获取年份 |
SELECT YEAR(CURDATE()) AS Year; |
MONTH |
获取月份 |
SELECT MONTH(CURDATE()) AS Month; |
DAY |
获取天数 |
SELECT DAY(CURDATE()) AS Day; |
HOUR |
获取小时 |
SELECT HOUR(CURTIME()) AS Hour; |
MINUTE |
获取分钟 |
SELECT MINUTE(CURTIME()) AS Minute; |
SECOND |
获取秒数 |
SELECT SECOND(CURTIME()) AS Second; |
WEEKDAY |
获取星期 |
SELECT WEEKDAY(CURDATE()) AS Weekday; |
DAYOFYEAR |
获取年的第几天 |
SELECT DAYOFYEAR(CURDATE()) AS DayOfYear; |
WEEK |
获取年的第几周 |
SELECT WEEK(CURDATE()) AS WeekOfYear; |
DATEDIFF |
计算日期差 |
SELECT DATEDIFF(CURDATE(), '2024-01-01') AS DateDifference; |
DATE_FORMAT |
日期格式化 |
SELECT DATE_FORMAT(CURDATE(), '%Y-%m-%d') AS DateFormat; |
DATE_ADD |
日期加 |
SELECT DATE_ADD(CURDATE(), INTERVAL 1 DAY) AS DateAdd; |
DATE_SUB |
日期减 |
SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) AS DateSub; |
数值处理
函数 |
说明 |
示例 |
ABS |
绝对值 |
SELECT ABS(-10) AS AbsoluteValue; |
CEIL |
向上取整 |
SELECT CEIL(3.14) AS CeilValue; |
FLOOR |
向下取整 |
SELECT FLOOR(3.14) AS FloorValue; |
ROUND |
四舍五入 |
SELECT ROUND(3.14) AS RoundValue; |
RAND |
随机数 |
SELECT RAND() AS RandomNumber; |
SQRT |
平方根 |
SELECT SQRT(16) AS SquareRoot; |
POW |
幂运算 |
SELECT POW(2, 3) AS Power; |
MOD |
取模运算 |
SELECT MOD(10, 3) AS Modulus; |
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至[email protected]。
文章标题:MySQL函数
字数:489
本文作者:LCX
发布时间:2024-09-14, 14:59:00
最后更新:2024-09-24, 08:51:51
原始链接:https://987654321.org/posts/mysql_function/
版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。