北京SEO

mysql你不知道字符串操作函数

2019/10/10/17:35:45  阅读:1830  来源:谷歌SEO算法  标签: 夫唯SEO视频教程

LENGTH()函数可以获得一个字符串的长度,代码如下:

  1. selectlength('abcdefg'),length('0123456789');
  2. /*
  3. +-------------------+----------------------+
  4. |length('abcdefg')|length('0123456789')|
  5. +-------------------+----------------------+
  6. |7|10|
  7. +-------------------+----------------------+
  8. 1rowinset(0.00sec)
  9. */

使用RPAD()或者LPAD()函数可以分别从右边或者左边对字符串进行填补,一般,字符串经常用空格进行填补,就是所谓的补齐,MySQL可以通过给RPAD()和LPAD()函数中指定一个自定义值作为第三个参数对字符串进行填补.

  1. selectrpad('simaopig',17,''),rpad('simaopig',17,'@'),lpad('simaopig',17,''),lpad('simaopig',17,'@');
  2. /*
  3. +------------------------+-------------------------+------------------------+-------------------------+
  4. |rpad('simaopig',17,'')|rpad('simaopig',17,'@')|lpad('simaopig',17,'')|lpad('simaopig',17,'@')|
  5. +------------------------+-------------------------+------------------------+-------------------------+
  6. |NULL|simaopig@@@@@@@@@|NULL|@@@@@@@@@simaopig|
  7. +------------------------+-------------------------+------------------------+-------------------------+
  8. 1rowinset(0.00sec)
  9. */
  10. selectrpad('simaopig',17,''),rpad('simaopig',17,'@'),lpad('simaopig',17,''),lpad('simaopig',17,'@');
  11. /*
  12. +-------------------------+-------------------------+-------------------------+-------------------------+
  13. |rpad('simaopig',17,'')|rpad('simaopig',17,'@')|lpad('simaopig',17,'')|lpad('simaopig',17,'@')|
  14. +-------------------------+-------------------------+-------------------------+-------------------------+
  15. |simaopig|simaopig@@@@@@@@@|simaopig|@@@@@@@@@simaopig|
  16. +-------------------------+-------------------------+-------------------------+-------------------------+
  17. 1rowinset(0.00sec)
  18. */

LTRIM()和RTRIM()函数产生与填补相反的效果,去除字符串的首和尾字符:

  1. selectltrim('simaopig'),ltrim('simaopig'),rtrim('simaopig'),rtrim('simaopig'),ltrim(rtrim('simaopig'));
  2. /*
  3. +----------------------+---------------------------+----------------------+--------------------------+----------------------------------+
  4. |ltrim('simaopig')|ltrim('simaopig')|rtrim('simaopig')|rtrim('simaopig')|ltrim(rtrim('simaopig'))|
  5. +----------------------+---------------------------+----------------------+--------------------------+----------------------------------+
  6. |simaopig|simaopig|simaopig|simaopig|simaopig|
  7. +----------------------+---------------------------+----------------------+--------------------------+----------------------------------+
  8. 1rowinset(0.00sec)
  9. */

即然谈到了"空格",那么把书中的例子再抄一遍,那就是MySQL提供了一个函数,这个函数(SPACE())的功能是只返回空格.

  1. selectspace(1),space(4),length(space(1)),length(space(4));
  2. /*
  3. +----------+----------+------------------+------------------+
  4. |space(1)|space(4)|length(space(1))|length(space(4))|
  5. +----------+----------+------------------+------------------+
  6. |||1|4|
  7. +----------+----------+------------------+------------------+
  8. 1rowinset(0.00sec)
  9. */

TRIM()函数可以指定去除格式,并且可以指定是去除左边还是右边?或者去除其它字符,不发局限于空格.

其实啊,就是把RTRIM()和LTRIM()的活都做了,而且做的好,可以说TRIM()和RTRIM()是TRIM()的子集,代码如下:

  1. selecttrim('simaopig'),length(trim('simaopig'))aslen;
  2. /*
  3. +--------------------+-----+
  4. |trim('simaopig')|len|
  5. +--------------------+-----+
  6. |simaopig|8|
  7. +--------------------+-----+
  8. 1rowinset(0.00sec)
  9. */
  10. selecttrim(leading'!'from'!!!simaopig!!!')astrim_leading,trim(trailing'!'from'!!!simaopig!!!')astrim_trailing,trim(both'!'from'!!!simaopig!!!')astrim_both;
  11. /*
  12. +--------------+---------------+-----------+
  13. |trim_leading|trim_trailing|trim_both|
  14. +--------------+---------------+-----------+
  15. |simaopig!!!|!!!simaopig|simaopig|
  16. +--------------+---------------+-----------+
  17. 1rowinset(0.00sec)
  18. */

LEFT()和RIGHT()函数可以获得字符串的指定部分,它将从字符串的左边或右边返回字符,代码如下:

  1. selectleft('simaopig',5)asleft_five,right('simaopig',5)asright_five,left(right('simaopig',7),5)asmidd_five;
  2. /*
  3. +-----------+------------+-----------+
  4. |left_five|right_five|midd_five|
  5. +-----------+------------+-----------+
  6. |simao|aopig|imaop|
  7. +-----------+------------+-----------+
  8. 1rowinset(0.00sec)
  9. */

SUBSTRING()函数,允许指定一个字符串的长度和起始位置,也是获取子字符串,注意哈,这里的MySQL的字符串起始位置是1,不是零哈,代码如下:

  1. selectsubstring('simaopig',2,3);
  2. /*
  3. +---------------------------+
  4. |substring('simaopig',2,3)|
  5. +---------------------------+
  6. |ima|
  7. +---------------------------+
  8. 1rowinset(0.00sec)
  9. */

CONCAT()函数使其内提供的参数连接起来,如果CONCAT()的参数是空的,其结果也是空的(NULL),代码如下:

  1. selectconcat('welcome','to','小小子'),concat('simaopig',NULL);
  2. /*
  3. +------------------------------------+-------------------------+
  4. |concat('welcome','to','小小子')|concat('simaopig',NULL)|
  5. +------------------------------------+-------------------------+
  6. |welcometo小小子|NULL|
  7. +------------------------------------+-------------------------+
  8. 1rowinset(0.00sec)
  9. */

CONCAT_WS()函数与CONCAT()函数基本相同,只是其还可以提供一个参数作为连接后字符串的分隔符,代码如下:

  1. selectconcat_ws(',','welcome','to','xiaoxiaozi.com')ascon_wel,concat_ws('','Simaopig','Love','Yatou')ascon_love;
  2. /*--phpfensi.com
  3. +---------------------------+---------------------+
  4. |con_wel|con_love|
  5. +---------------------------+---------------------+
  6. |welcome,to,xiaoxiaozi.com|SimaopigLoveYatou|
  7. +---------------------------+---------------------+
  8. 1rowinset(0.00sec)
  9. */

使用LOCATE()函数检验一个字符串是否包含所要检测的字符串,如果存在,返回其第一次出现的位置,今天,第二次强调,MySQL字符串起始位置为1,不是零,代码如下:

  1. selectlocate('pig','simaopig'),locate('','simaopig'),locate('','simaopig'),locate('','simaopig');
  2. /*
  3. +--------------------------+-----------------------+------------------------+--------------------------+
  4. |locate('pig','simaopig')|locate('','simaopig')|locate('','simaopig')|locate('','simaopig')|
  5. +--------------------------+-----------------------+------------------------+--------------------------+
  6. |6|1|0|1|
  7. +--------------------------+-----------------------+------------------------+--------------------------+
  8. 1rowinset(0.00sec)
  9. */

FIND_IN_SET()函数是在一个大集合中查找指定的字符串,在MySQL中看到set就应该想到集合,代码如下:

  1. selectfind_in_set('simaopig','My,Name,Is,Simaopig,You,Can,Call,Me,simaopig,Too')asstring_locate;
  2. /*
  3. +---------------+
  4. |string_locate|
  5. +---------------+
  6. |4|
  7. +---------------+
  8. 1rowinset(0.00sec)
  9. */

哈,看到了吧,其返回的是该字符串是集合的第几个元素,嗯,你还问为啥大写的也返回了?呃,因为我没有用binary啊,STRCMP()函数比较两个字符串是否相同,相同返回0,如果第一个大于第二个返回1,否则返回-1.

呃,同样的,不加binary的话,字符串比较是不区分大小写的,代码如下:

  1. selectstrcmp('a','b'),strcmp('b','a'),strcmp('a','a'),strcmp('a','A'),strcmp('a','B');
  2. /*
  3. +-----------------+-----------------+-----------------+-----------------+-----------------+
  4. |strcmp('a','b')|strcmp('b','a')|strcmp('a','a')|strcmp('a','A')|strcmp('a','B')|
  5. +-----------------+-----------------+-----------------+-----------------+-----------------+
  6. |-1|1|0|0|-1|
  7. +-----------------+-----------------+-----------------+-----------------+-----------------+
  8. 1rowinset(0.00sec)
  9. */

REPLACE()函数和在别的编程语言里的用法一致,替换功能,代码如下:

  1. selectreplace('Iamsimaopig','simaopig','xiaoxiaozi');
  2. /*
  3. +--------------------------------------------------+
  4. |replace('Iamsimaopig','simaopig','xiaoxiaozi')|
  5. +--------------------------------------------------+
  6. |Iamxiaoxiaozi|
  7. +--------------------------------------------------+
  8. 1rowinset(0.00sec)
  9. */

INSSERT()函数可以把一个字符串的指定部分,定义了起始位置和长度,替换为一个新值,代码如下:

  1. selectinsert('Iamsimaopig',6,8,'xiaoxiaozi');
  2. /*
  3. +------------------------------------------+
  4. |insert('Iamsimaopig',6,8,'xiaoxiaozi')|
  5. +------------------------------------------+
  6. |Iamxiaoxiaozi|
  7. +------------------------------------------+
  8. 1rowinset(0.00sec)
  9. */

REPEAT()函数用于重复字符串操作,例如,输出十遍'xiaoxiaozi',代码如下:

  1. selectrepeat('xiaoxiaozi',10);
  2. /*
  3. +------------------------------------------------------------------------------------------------------+
  4. |repeat('xiaoxiaozi',10)|
  5. +------------------------------------------------------------------------------------------------------+
  6. |xiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozi|
  7. +------------------------------------------------------------------------------------------------------+
  8. 1rowinset(0.00sec)
  9. */

REVERSE()函数颠倒字符串,代码如下:

  1. selectreverse('simaopig'),reverse(repeat('hello',6));
  2. /*
  3. +---------------------+--------------------------------------+
  4. |reverse('simaopig')|reverse(repeat('hello',6))|
  5. +---------------------+--------------------------------------+
  6. |gipoamis|olleholleholleholleholleholleh|
  7. +---------------------+--------------------------------------+
  8. 1rowinset(0.00sec)
  9. */

UCASE()和LCASE()将字符串分别转化为大写和小写,代码如下:

  1. selectucase('simaopig'),ucase('Simaopig'),lcase('SIMAOPIG'),lcase('simaopiG');
  2. /*
  3. +-------------------+-------------------+-------------------+-------------------+
  4. |ucase('simaopig')|ucase('Simaopig')|lcase('SIMAOPIG')|lcase('simaopiG')|
  5. +-------------------+-------------------+-------------------+-------------------+
  6. |SIMAOPIG|SIMAOPIG|simaopig|simaopig|
  7. +-------------------+-------------------+-------------------+-------------------+
  8. 1rowinset(0.00sec)
  9. */

ASCII()函数返回指定字符的ASCII码,代码如下:

  1. selectascii('y'),ascii('Simaopig'),ascii('simaopig');
  2. /*
  3. +------------+-------------------+-------------------+
  4. |ascii('y')|ascii('Simaopig')|ascii('simaopig')|
  5. +------------+-------------------+-------------------+
  6. |121|83|115|
  7. +------------+-------------------+-------------------+
  8. 1rowinset(0.00sec)
  9. */

ORD()函数返回指定字符的数字编码,经常用于替代ASCII(),我咋感觉其返回值与ASCII()一模一样,PHP也有这个函数,貌似,代码如下:

  1. selectord('y'),ord('Simaopig'),ord('simaopig');
  2. /*
  3. +----------+-----------------+-----------------+
  4. |ord('y')|ord('Simaopig')|ord('simaopig')|
  5. +----------+-----------------+-----------------+
  6. |121|83|115|
  7. +----------+-----------------+-----------------+
  8. 1rowinset(0.00sec)
  9. */

再补充一下

1.ASCII(str)

返回字符串str的最左面字符的ASCII代码值,如果str是空字符串,返回0,如果str是NULL,返回NULL.

  1. mysql>selectASCII('2');
  2. ->50
  3. mysql>selectASCII(2);
  4. ->50
  5. mysql>selectASCII('dx');
  6. ->100也可参见ORD()函数。

2.ORD(str)

如果字符串str最左面字符是一个多字节字符,通过以格式((first byte ASCII code)*256+(second byte ASCII code))[*256+third byte ASCII code...]返回字符的ASCII代码值来返回多字节字符代码。如果最左面的字符不是一个多字节字符。返回与ASCII()函数返回的相同值。

mysql> select ORD('2');

-> 50

3.CONV(N,from_base,to_base)

在不同的数字基之间变换数字,返回数字N的字符串数字,从from_base基变换为to_base基,如果任何参数是NULL,返回NULL,参数N解释为一个整数,但是可以指定为一个整数或一个字符串,最小基是2且最大的基是36,如果to_base是一个负数,N被认为是一个有符号数,否则,N被当作无符号数,CONV以64位点精度工作.

  1. mysql>selectCONV("a",16,2);
  2. ->'1010'
  3. mysql>selectCONV("6E",18,8);
  4. ->'172'
  5. mysql>selectCONV(-17,10,-18);
  6. ->'-H'
  7. mysql>selectCONV(10+"10"+'10'+0xa,10,10);
  8. ->'40'

4.BIN(N)

返回二进制值N的一个字符串表示,在此N是一个长整数(BIGINT)数字,这等价于CONV(N,10,2),如果N是NULL,返回NULL.

mysql> select BIN(12);

-> '1100'

5.OCT(N)

返回八进制值N的一个字符串的表示,在此N是一个长整型数字,这等价于CONV(N,10,8)。如果N是NULL,返回NULL.

mysql> select OCT(12);

-> '14'

6.HEX(N)

返回十六进制值N一个字符串的表示,在此N是一个长整型(BIGINT)数字,这等价于CONV(N,10,16),如果N是NULL,返回NULL.

mysql> select HEX(255);

-> 'FF'

7.CHAR(N,...)

CHAR()将参数解释为整数并且返回由这些整数的ASCII代码字符组成的一个字符串,NULL值被跳过.

  1. mysql>selectCHAR(77,121,83,81,'76');
  2. ->'MySQL'
  3. mysql>selectCHAR(77,77.3,'77.3');
  4. ->'MMM'

广告内容

mysql你不知道字符串操作函数 mysql你不知道字符串操作函数 mysql你不知道字符串操作函数

相关阅读

热门评论

SEO探索者团队 SEO探索者团队

SEO服务&网站优化

总篇数182

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法