MySQL(三)

发布于:2024-12-05 ⋅ 阅读:(373) ⋅ 点赞:(0)

1、内置函数

1.1、日期函数

current_date() 当前的日期
current_time() 当前的时间
current_timestamp() 当前的时间戳,即日期+时间
date(datetime) 返回参数中的日期部分
date_add(date ,interval x date_type) 在date的基础上增加x时间,date_type的单位可以是year、minute、second、day
date_sub(date ,interval x date_type) 在date的基础上减少x时间,date_type的单位可以是year、minute、second、day
datediff(date1 , date2) 计算date1和date2的差,单位是天
now() 当前的日期时间

如:

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-12-02 16:58:29 |
+---------------------+
1 row in set (0.00 sec)

mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2024-12-02     |
+----------------+
1 row in set (0.00 sec)

mysql> select date_add(now(), interval 1 day);
+---------------------------------+
| date_add(now(), interval 1 day) |
+---------------------------------+
| 2024-12-03 17:00:24             |
+---------------------------------+
1 row in set (0.01 sec)

1.2、字符串函数

charset(str) 返回字符串使用的字符编码集
concat(string1, string2, ...) 连接字符串
instr(string, sub_string) 返回sub_string在string中出现的位置,没有出现就返回0
ucase(string) 转为大写
lcase(string) 转为小写
left(string, length) 从string中的左边取length个字符
right(string, length) 从string中的右边取length个字符
length(string) string的长度
replace(string, search_string, replace_string) 将string中的search_string替换为replace_string
strcmp(string1, string2) 逐个字符的比较string1和string2的大小
substring(string, position, length) 从string的position位置开始,取length个字符(不写length默认到结尾)
ltrim(string) 去除前空格
rtrim(string) 去除后空格
trim(string) 同时去除前后空格

如:

mysql> select charset("abcd");
+-----------------+
| charset("abcd") |
+-----------------+
| utf8mb4         |
+-----------------+
1 row in set (0.02 sec)

mysql> select concat("abc", "nihao", "hello");
+---------------------------------+
| concat("abc", "nihao", "hello") |
+---------------------------------+
| abcnihaohello                   |
+---------------------------------&#

网站公告

今日签到

点亮在社区的每一天
去签到