当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL笔记之字符串函数的应用

MySQL笔记之字符串函数的应用

2017年12月12日  | 移动技术网IT编程  | 我要评论

字符串操作在程序设计中是非常重要的组成部分,而mysql数据库中的字符串操作却相当简单

需要注意的是:下面所有的函数只是将修改后的结果当查询返回,均不对原数据做出改变


选出指定数量字符

复制代码 代码如下:

mysql> select right('nihao',3);
+------------------+
| right('nihao',3) |
+------------------+
| hao              |
+------------------+
 row in set (0.00 sec)

这里的right()函数代表从字符串中选定从右往左数的三个字符

与此类似,还有left()函数


substring_index截取字符串

复制代码 代码如下:

mysql> select substring_index('hh,mm,ss',',',2);
+-----------------------------------+
| substring_index('hh,mm,ss',',',2) |
+-----------------------------------+
| hh,mm                             |
+-----------------------------------+
 row in set (0.00 sec)

此函数内部的第一个参数代表需要截取的内容,第二个参数代表按什么截取

最后一个是截取到第几个,1是截取到第一个逗号,2是截取到第二个


substring截取字符串

复制代码 代码如下:

mysql> select substring('helloworld',1,5);
+-----------------------------+
| substring('helloworld',1,5) |
+-----------------------------+
| hello                       |
+-----------------------------+
 row in set (0.00 sec)

此处截取字符串中1~5的内容

upper字符串改大写
复制代码 代码如下:

mysql> select upper('hello');
+----------------+
| upper('hello') |
+----------------+
| hello          |
+----------------+
 row in set (0.00 sec)

lower字符串改小写

复制代码 代码如下:

mysql> select lower('hello');
+----------------+
| lower('hello') |
+----------------+
| hello          |
+----------------+
 row in set (0.00 sec)

reverse反转字符串

复制代码 代码如下:

mysql> select reverse('hello');
+------------------+
| reverse('hello') |
+------------------+
| olleh            |
+------------------+
 row in set (0.00 sec)

ltrim清除左边多余空格

复制代码 代码如下:

mysql> select ltrim('  hello     ');
+-----------------------+
| ltrim('  hello     ') |
+-----------------------+
| hello                 |
+-----------------------+
 row in set (0.00 sec)

此外还有rtrim清除右边空格,trim清除两边全部空格

length返回字符串中的字符数量

复制代码 代码如下:

mysql> select length('helo');
+----------------+
| length('helo') |
+----------------+
|              4 |
+----------------+
 row in set (0.00 sec)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网