zoukankan      html  css  js  c++  java
  • mysql字符串拼接

    1. CONCAT()
    2. CONCAT_WS()
    3. GROUP_CONCAT()

    update  xxx.table  set column= CONCAT('DBxx',device_code)  where column like 'DBxx-xxx%';

    为了方便下面举例,这里放个student表供下面使用

    s_id s_name s_sex
    01 张三 男
    02 李四 男
    03 王五 男
    04 赵六 null
    一、CONCAT() :
    最常用的字符串拼接方法,但遇到拼接中的字符串出现null的情况会返回null
    语法:CONCAT(string1,string2)
    DEMO1
    mysql > SELECT CONCAT(s_name,s_sex) FROM student
    +----------------------+
    CONCAT(s_name,s_sex)
    +----------------------+
    张三男
    赵四男
    王五男
    null

    1
    2
    3
    4
    5
    6
    7
    8
    9
    二、CONCAT_WS():concat with separator
    比CONCAT的优点
    多了个分隔符功能
    如果某个字符串为null,会忽略null,并返回其他字符串的值
    语法:CONCAT_WS(separator,str1,str2,…)
    代表 concat with separator ,是concat()的特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数
    DEMO2
    mysql > SELECT CONCAT_WS('--',s_name,s_sex) FROM student
    +-------------------------------+
    CONCAT_WS('--',s_name,s_sex)
    +-------------------------------+
    张三--男
    赵四--男
    王五--男
    赵六

    1
    2
    3
    4
    5
    6
    7
    8
    9
    三、GROUP_CONCAT()
    连接字段,多个值显示为一行
    语法 :group_concat( [DISTINCT] 连接的字段 [Order BY 排序字段 ASC/DESC] [Separator ‘分隔符’] )
    连接的可以是多个字段,也可以对连接字段进行排序
    DEMO3:
    mysql > SELECT GROUP_CONCAT(s_id) FROM student
    +--------------------------------------------+
    GROUP_CONCAT(s_id)
    +--------------------------------------------+
    01,02,03
    1
    2
    3
    4
    5
    DEMO4:连接多个字段,并以其中一个字段排序
    mysql > SELECT GROUP_CONCAT(s_id,s_name order by s_id desc) FROM student
    +--------------------------------------------+
    GROUP_CONCAT(s_id,s_name order by s_id desc)
    +--------------------------------------------+
    03王五,02赵四,01张三
    ————————————————
     https://blog.csdn.net/cpc784221489/article/details/92774446

  • 相关阅读:
    osgi:设置httpservice端口号
    osgi: HttpService A null service reference is not allowed.
    Java Web中涉及的编解码
    http协议之cookie标准RFC6265介绍
    信息传输完整性、保密性、不可抵赖性实现
    web开发基础--字节序
    结构体
    ArrayList和LinkedList的some东东
    循环---匹配
    关于游戏的留存率想到的
  • 原文地址:https://www.cnblogs.com/maxforb/p/11555622.html
Copyright © 2011-2022 走看看