• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

mysql 常用的三类函数

mysql 搞代码 4年前 (2022-01-09) 20次浏览 已收录 0个评论

本篇文章主要介绍mysql 常用的三类函数,感兴趣的朋友参考下,希望对大家有所帮助。

一、字符串类。

注:mysql在处理字符串时,字符下标从1开始。

1、concat(string1, string2, ……); //连接字符串

mysql> select concat('leng', 'xue', 'gang') as name;
+————-+
| name |
+————-+
| lengxuegang |
+————-+
1 row in set (0.00 sec)

2、instr(string, substring); //返回substring首次在string中出现的位置,不存在返回0

mysql> select instr('lengxuegang', 'xue');
+—————————–+
| instr('lengxuegang', 'xue') |
+—————————–+
| 5 |
+—————————–+
1 row in set (0.00 sec)

mysql> select instr('lengxuegang', 'none');
+——————————+
| instr('lengxuegang', 'none') |
+——————————+
| 0 |
+——————————+
1 row in set (0.00 sec)

3、lcase(string); //转换为小写

mysql> select lcase('LengxueGang');
+———————-+
| lcase('LengxueGang') |
+———————-+
| lengxuegang |
+———————-+
1 row in set (0.00 sec)

4、left(string, length); //从string左边起取length个字符

mysql> select left('lengxue本文来源gao@daima#com搞(%代@#码网@gang', 4);
+————————+
| left('lengxuegang', 4) |
+————————+
| leng |
+————————+
1 row in set (0.01 sec)

5、length(string); //返回string的长度

mysql> select length('lengxuegang');
+———————–+
| length('lengxuegang') |
+———————–+
| 11 |
+———————–+
1 row in set (0.25 sec)

6、locate(substring, string, [start_position]); //从start_position出开始查找,返回substring在string中首次出现的位置。其功能与instr类似,不过注意string与substring的位置是不一样的。

mysql> select locate('leng', 'lengxueganglengxuegang', 4);
+———————————————+
| locate('leng', 'lengxueganglengxuegang', 4) |
+———————————————+
| 12 |
+———————————————+
1 row in set (0.00 sec)

7、ltrim(string); //去除左边的空格

mysql> select ltrim(' leng');
+——————+
| ltrim(' leng') |
+——————+
| leng |
+——————+
1 row in set (0.00 sec)

8、repeat(string, count); //重复string count次

mysql> select repeat('leng', 4);
+——————-+
| repeat('leng', 4) |
+——————-+
| lenglenglengleng |
+——————-+
1 row in set (0.00 sec)

9、replace(string, search_str, replace_str); //在string中将search_str替换为replace_str

mysql> select replace('lengxueganglengxuegang', 'leng', 'cheng');
+—————————————————-+
| replace('lengxueganglengxuegang', 'leng', 'cheng') |
+—————————————————-+
| chengxuegangchengxuegang |
+—————————————————-+
1 row in set (0.05 sec)

10、rtrim(string); //去除右端空格

mysql> select rtrim('leng ');
+——————–+
| rtrim('leng ') |
+——————–+
| leng |
+——————–+
1 row in set (0.00 sec)


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:mysql 常用的三类函数

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址