MySQL—常用函数

这篇博客详细介绍了MySQL中的常用函数,包括字符串函数如concat、insert、lower等,数值函数如abs、ceil、mod,日期函数如curdate、now、date_format,流程函数如if、ifnull,以及其他函数如database、version等。对于每个函数,都提供了具体的功能和示例,帮助读者理解和运用。
摘要由CSDN通过智能技术生成

1、字符串函数

  • concat(s1,s2,…sn)

将传入的参数连成字符串,如果与null相连,则值也为null

mysql> select concat('aaa','bbb','ccc'),concat('aaa',null);
+---------------------------+--------------------+
| concat('aaa','bbb','ccc') | concat('aaa',null) |
+---------------------------+--------------------+
| aaabbbccc                 | NULL               |
+---------------------------+--------------------+
1 row in set (0.00 sec)
  • insert(str,x,y,instr)

将字符串str从第x位开始,y个字符长的子串替换为instr

mysql> select insert('hello world',2,4,'ll');
+--------------------------------+
| insert('hello world',2,4,'ll') |
+--------------------------------+
| hll world                      |
+--------------------------------+
1 row in set (0.00 sec)
  • lower(str),upper(str)

将字符串str转为小写、大写

mysql> select lower('HELLO'),upper('hello');
+----------------+----------------+
| lower('HELLO') | upper('hello') |
+----------------+----------------+
| hello          | HELLO          |
+----------------+----------------+
1 row in set (0.00 sec)
  • left(str,x),right(str,x)

返回字符串str最左边、最右边x个字符

mysql> select left('hello',2),right('hello',2);
+-----------------+------------------+
| left('hello',2) | right('hello',2) |
+-----------------+------------------+
| he              | lo               |
+-----------------+------------------+
1 row in set (0.00 sec)
  • substring(str,x,y)

返回从字符串str中的第x位开始,y字符长度的子串

mysql> select substring('hello world',5,3);
+------------------------------+
| substring('hello world',5,3) |
+------------------------------+
| o w                          |
+------------------------------+
1 row in set (0.00 sec)
  • lpad(str,n,pad),rpad(str,n,pad)

用字符串pad对str最左边、最右边进行填充,直到长度达到n位为止

mysql> select lpad('hello',18,'world'),rpad('hello',18,'world');
+--------------------------+--------------------------+
| lpad('hello',18,'world') | rpad('hello',1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值