MySQL常用函数

拼接字符串

1.CONCAT(s1,s2...sn)

字符串 s1,s2 等多个字符串合并为一个字符串。

SELECT CONCAT("str1", "str2") AS str;
+----------+
| str      |
+----------+
| str1str2 |
+----------+

2.CONCAT_WS(x, s1,s2...sn)

同 CONCAT(s1,s2,...) 函数,但是每个字符串之间要加上 x,x 可以是分隔符。

SELECT CONCAT_WS("-", "str1", "str2", "str3")AS str;
+----------------+
| str            |
+----------------+
| str1-str2-str3 |
+----------------+

翻转字符串REVERSE(s)

SELECT REVERSE('ABCDE');
+------------------+
| REVERSE('ABCDE') |
+------------------+
| EDCBA            |
+------------------+

字符串截取

1.SUBSTR(s, start, length)

从字符串 s 的 start 位置截取长度为 length 的子字符串

SELECT SUBSTR("asdfgh", 2, 3) AS new_str;
+---------+
| new_str |
+---------+
| sdf     |
+---------+

2.SUBSTRING(s, start, length)

从字符串 s 的 start 位置截取长度为 length 的子字符串

SELECT SUBSTRING("asdfgh", 2, 3) AS new_str;
+---------+
| new_str |
+---------+
| sdf     |
+---------+

大小写转换

1.UCASE(s)

将字符串转换为大写。

mysql> SELECT UCASE("asdfgh");
+-----------------+
| UCASE("asdfgh") |
+-----------------+
| ASDFGH          |
+-----------------+

2.UPPER(s)

将字符串转换为大写。

mysql> SELECT UPPER("asdfgh");
+-----------------+
| UPPER("asdfgh") |
+-----------------+
| ASDFGH          |
+-----------------+
···

[参考](https://www.runoob.com/mysql/mysql-functions.html)
posted @ 2021-04-28 00:26  万般自然  阅读(57)  评论(0编辑  收藏  举报