大纲


    首页 mysql基础教程[basic] 详情
    mysql字符串函数

    总结了字符串函数的用法

    函数列表

    函数 功能
    concat(s1, s2, ... , sn) 连接s1, s2, ..., sn 为一个字符串
    insert(str, x, y, instr) 将字符串str从第x位置开始, y个字符长度的子字符串替换为字符串instr
    lower(str) 将字符串str中所有的字符转换为小写
    upper(str) 将字符串str中所有的字符转换为大写
    left(str, x) 返回字符串str最左边的x个字符
    right(str, y) 返回字符串str最右边的y个字符
    lpad(str, n, pad) 用字符串pad对str最左边进行填充, 直到长度为n个字符长度
    rpad(str, n, pad) 用字符串pad对str最右边进行填充, 直到长度为n个字符长度
    trim(str) 去掉字符串str两边的空格
    ltrim(str) 去掉str中最左边的空格
    rtrim(str) 去掉str中最右边的空格
    repeat(str, x) 返回str中重复出现x次的结果
    replace(str, a, b) 将字符串str中的a更换为b
    strcmp(s1, s2) 比较字符串s1, s2
    如果两个字符串是一样的则返回0;如果第一个小于第二个则返回-1;否则返回1
    substring 返回字符串str x位置开始y个字符长度的字符串
    length(str) 字符的字节长度
    char_length(str) 字符串的字符个数
    space(N) N个空格组成的空串
    instr(str,substr) 子串substr第一次出现的位置
    reverse(str) 反转字符串
    format(x,d[,locale]) 以格式‘#,###,###.##’格式化数字X
    D指定小数位数
    locale指定国家语言(默认的locale为en_US)

    group_concat( )

    • group_concat( [DISTINCT]  要连接的字段   [Order BY 排序字段 ASC/DESC]   [Separator '分隔符'] )
    select * from goods;  
    +------+------+
    | id| price|
    +------+------+
    |1 | 10|
    |1 | 20|
    |1 | 20|
    |2 | 20|
    |3 | 200 |
    |3 | 500 |
    +------+------+
    6 rows in set (0.00 sec)
    
    -- 以id分组,把price字段的值在同一行打印出来,逗号分隔(默认)
    
    select id,group_concat(price) from goods group by id;  
    +------+--------------------+
    | id| group_concat(price) |
    +------+--------------------+
    |1 | 10,20,20|
    |2 | 20 |
    |3 | 200,500|
    +------+--------------------+
    3 rows in set (0.00 sec)
    
    -- 以id分组,把price字段的值在一行打印出来,分号分隔 
    
    select id,group_concat(price order by price separator ',') as comb
    from goods group by id;  
    +------+----------------------------------+
    | id| comb |
    +------+----------------------------------+
    |1 | 10;20;20 |
    |2 | 20|
    

    group_concat 的逆操作

    -- 如果group_concat形成的字段是按照 , 分割的
    -- 可以通过 find_in_set 函数逆回去
    select 
        ...
    from tbl1 t1  -- 全量表
    join tbl_concated t2  -- group_concat的表
    on find_in_set(t1.id,t2.ids)
    

    trim详细用法

    • TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str)
    • 从源字符串str中去掉两端、前缀或后缀字符remstr并返回
    • 如果不指定remstr,则去掉str两端的空格
    mysql> select trim('  bar  ');
    +-----------------+
    | trim('  bar  ') |
    +-----------------+
    | bar             |
    +-----------------+
    
    mysql> select trim(leading 'x' from 'xxxbarxxx');
    +------------------------------------+
    | trim(leading 'x' from 'xxxbarxxx') |
    +------------------------------------+
    | barxxx                             |
    +------------------------------------+
    
    mysql> select trim(both 'x' from 'xxxbarxxx');
    +---------------------------------+
    | trim(both 'x' from 'xxxbarxxx') |
    +---------------------------------+
    | bar                             |
    +---------------------------------+
    
    mysql> select trim(trailing 'xyz' from 'barxxyz');
    +-------------------------------------+
    | trim(trailing 'xyz' from 'barxxyz') |
    +-------------------------------------+
    | barx                                |
    +-------------------------------------+
    
    评论
    您尚未登录,请 登录 后评论
    共 0 条评论 | 欢迎尬评