[摘要]今天在论坛中看到一个关于mysql的问题,问题如下good_id cat_id12654 665,56912655 601,4722goods_id是商品...
今天在论坛中看到一个关于mysql的问题,问题如下
good_id cat_id
12654 665,569
12655 601,4722
goods_id是商品id
cat_id是分类id
当我,怎么根据这种分类ID查数据(一个商品有多个分类,而且用逗号隔开了)
我现在用的是like 这样的话,输入一个分类id是688,或者4722都能出来这个商品,但输入一个722也出来这个商品了。
如果用like做的话,肯定会有问题的,我的开始的想法是,把cat_id里面的字符串换成数组,这样可以利用mysql里面的in操作,这样就不会出现查找722,而4722类别下的产品都跑出来了。我从网上找了半天,这方面的字符串操作函数,没找到,不过我发现了find_in_set这个函数虽然不能,将字符串转换成数组,但是也不会出现上面的情况。我发现自己有好多函数不知道,所以我从手册中,以及网上收集了半天,做了一些例子。
CHAR_LENGTH(str) 返回str的字符数

LENGTH(str) 返回str的字节长度

CONCAT(str1,str2....) 返回参数str1,str2....的拼接,有一个null值则返回null

CONCAT_WS(x,str1,str2...) ---concat with separator 第一个参数x代表分隔符,后面的参数为字符串
如果x为NULL则返回NULL,如果字符串有NULL会被忽略

INSERT(s1,x,len,s2) 字符串替换 s1 目标字符串 x为s1的开始位置(下标从1开始) len为x之后的长度 s2为需要替换的字符串
如果x超过s1的长度下标,则返回s1,如果len大于x之后剩余字符串长度,则全部替换

LOWER(str)、LCASE(str) 字符串转为小写

UPPER(str)、UCASE(str) 字符串转为大写

LEFT(str,n) 返回字符串str最左边的n个字符

RIGHT(str,n) 返回字符串str最右边的n个字符

LPAD(s1,len,s2) 返回s2从s1的左边填充直到总长度为len,如果len<s1的长度,则s2不填充,返回s1从左开始的len长度字符

RPAD(s1,len,s2) 返回s2从s1的右边填充直至len的长度,如果len<s1的长度,则s2不填充,返回s1从左开始的len长度字符

LTRIM(S) 删除左侧的空格 RTRIM(s) 删除右侧的空格 TRIM(s) 删除两侧的空格

TRIM(s1 FROM s2) 删除s2两端出现的s1字符串

REPEAT(s,n) 返回n个s组成的字符串,n<=0,返回空;s或者n为NULL,返回NULL

SPACE(n) 返回n个空格字符串

REPLACE(s,s1,s2) 返回--由s2替换s中的所有s1字符串的结果

STRCPM(s1,s2) s1==s2 返回0 s1<s2 返回-1 s1>s2 返回1

SUBSTRING(s,n,len) 、MID(s,n,len)返回s从n开始的len长度的字符串 ,若n<0,则从s的最后倒数n个开始截取,n=0的话返回空字符串;len可以省略-截取n之后的所有字符


LOCATE(str1,str2)、POSITION(str1 IN str2)、INSTR(str2,str1) 返回str1在str2中出现的开始位置

REVERSE(s) 返回s的字符串的反转

ELT(n,str1,str2,str3...) 返回str1,str2.. 中的第n个字符串 ,n超出返回返回null

FIELD(s,str1,str2,str3....) 返回s在str1,str2.....中的第一次出现的位置,没有返回0

FIND_IN_SET(s,str1) 返回s在str1中的出现的位置,其中str1由多个字符串组成,逗号隔开的列表

以上就是MySQL中字符串函数的详解的详细内容,更多请关注php中文网其它相关文章!
微信
分享
学习教程快速掌握从入门到精通的SQL知识。
关键词:MySQL中字符串函数的详细说明