6个常用的Mysql字符串连接函数

2019-03-13 15:46|来源: 网络

有时直接用mysql的字符串函数比查出来再用java字符串函数来处理简单的多,在这里收集了6个常用的Mysql字符串连接函数,以备有用之时。


1、concat()函数
  1.1 MySQL的concat函数可以连接一个或者多个字符串,如
      mysql> select concat('10');
      +--------------+
      | concat('10') |
      +--------------+
      | 10           |
      +--------------+
      1 row in set (0.00 sec)


      mysql> select concat('11','22','33');
      +------------------------+
      | concat('11','22','33') |
      +------------------------+
      | 112233                 |
      +------------------------+
      1 row in set (0.00 sec)


      而Oracle的concat函数只能连接两个字符串
      SQL> select concat('11','22') from dual;


  1.2 MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
      mysql> select concat('11','22',null);
      +------------------------+
      | concat('11','22',null) |
      +------------------------+
      | NULL                   |
      +------------------------+
      1 row in set (0.00 sec)


      而Oracle的concat函数连接的时候,只要有一个字符串不是NULL,就不会返回NULL


      SQL> select concat('11',NULL) from dual;

      CONCAT
      --
      11


2、concat_ws()函数, 表示concat with separator,即有分隔符的字符串连接
  如连接后以逗号分隔
      mysql> select concat_ws(',','11','22','33');
      +-------------------------------+
      | concat_ws(',','11','22','33') |
      +-------------------------------+
      | 11,22,33                      |
      +-------------------------------+
      1 row in set (0.00 sec)  


  和concat不同的是, concat_ws函数在执行的时候,不会因为NULL值而返回NULL
      mysql> select concat_ws(',','11','22',NULL);
      +-------------------------------+
      | concat_ws(',','11','22',NULL) |
      +-------------------------------+
      | 11,22                         |
      +-------------------------------+
      1 row in set (0.00 sec)


3、group_concat()可用来行转列, Oracle没有这样的函数
  完整的语法如下
  group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])


  如下例子
  mysql> select * from aa;
  +------+------+
  | id   | name |
  +------+------+
  |    1 | 10   |
  |    1 | 20   |
  |    1 | 20   |
  |    2 | 20   |
  |    3 | 200  |
  |    3 | 500  |
  +------+------+
  6 rows in set (0.00 sec)


  3.1 以id分组,把name字段的值打印在一行,逗号分隔(默认)
      mysql> select id,group_concat(name) from aa group by id;
      +------+--------------------+
      | id   | group_concat(name) |
      +------+--------------------+
      |    1 | 10,20,20           |
      |    2 | 20                 |
      |    3 | 200,500            |
      +------+--------------------+
      3 rows in set (0.00 sec)


  3.2 以id分组,把name字段的值打印在一行,分号分隔
      mysql> select id,group_concat(name separator ';') from aa group by id;
      +------+----------------------------------+
      | id   | group_concat(name separator ';') |
      +------+----------------------------------+
      |    1 | 10;20;20                         |
      |    2 | 20                               |
      |    3 | 200;500                          |
      +------+----------------------------------+
      3 rows in set (0.00 sec)


  3.3 以id分组,把去冗余的name字段的值打印在一行,逗号分隔
      mysql> select id,group_concat(distinct name) from aa group by id;
      +------+-----------------------------+
      | id   | group_concat(distinct name) |
      +------+-----------------------------+
      |    1 | 10,20                       |
      |    2 | 20                          |
      |    3 | 200,500                     |
      +------+-----------------------------+
      3 rows in set (0.00 sec)


  3.4 以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序
      mysql> select id,group_concat(name order by name desc) from aa group by id;
      +------+---------------------------------------+
      | id   | group_concat(name order by name desc) |
      +------+---------------------------------------+
      |    1 | 20,20,10                              |
      |    2 | 20                                    |
      |    3 | 500,200                               |
      +------+---------------------------------------+
      3 rows in set (0.00 sec)


4、repeat()函数,用来复制字符串,如下'ab'表示要复制的字符串,2表示复制的份数
  mysql> select repeat('ab',2);
  +----------------+
  | repeat('ab',2) |
  +----------------+
  | abab           |
  +----------------+
  1 row in set (0.00 sec)


  又如
  mysql> select repeat('a',2);
  +---------------+
  | repeat('a',2) |
  +---------------+
  | aa            |
  +---------------+
  1 row in set (0.00 sec)

5、SPACE(N) 函数。生成N个空格,如
  mysql> select space(3);
  +----------+
  | space(3) |
  +----------+
  |          |
  +----------+
  1 row in set (0.00 sec)


6、STRCMP(STR1,STR2) 字符串比较函数,该函数和字符集有关系,默认区分大小写
  若STR1和STR2相同, 则返回 0,
  若STR1小于STR2,   则返回 -1,
  若STR1大于STR2,   则返回 1
  mysql> select strcmp('abc','abc');
  +---------------------+
  | strcmp('abc','abc') |
  +---------------------+
  |                   0 |
  +---------------------+
  1 row in set (0.00 sec)


  mysql> select strcmp('a','ab');
  +------------------+
  | strcmp('a','ab') |
  +------------------+
  |               -1 |
  +------------------+
  1 row in set (0.00 sec)


  mysql> select strcmp('abc','ab');
  +--------------------+
  | strcmp('abc','ab') |
  +--------------------+
  |                  1 |
  +--------------------+
  1 row in set (0.00 sec)


本文链接:6个常用的Mysql字符串连接函数,参考网络资源

相关问答

更多
  • 只是输出效果的话,没必要strcat,不要用puts,改printf,格式不换行就可以 要用strcat,需要再定义一个b,先strcpy b a 执行fun后,再strcat b a
  • 由于聚合字符串可能超过4000字节,因此不能使用LISTAGG功能。 您可能会创建一个用户定义的聚合函数 ,该函数返回CLOB而不是VARCHAR2 。 有一个用户定义的聚合的例子,它在Tim从第一次讨论中链接到的原始askTom讨论中返回一个CLOB 。 Since the aggregates string can be longer than 4000 bytes, you can't use the LISTAGG function. You could potentially create a u ...
  • 最明显的原因可能是PHP从Perl继承了它的很多语法 - 而Perl使用点( . )作为字符串连接。 但是,我们可以深入研究并找出为什么这是在Perl中实现的 - +运算符最常用于数学等式 - 它仅用于语言中的连接,其中变量类型可以定义运算符工作的方式(简单的解释,例子是C#) var intAddition = 1 + 2; Console.WriteLine(intAddition); // Prints 3 var stringConcat = "1" + "2"; Console.WriteLine ...
  • 简单的+运算符是最快的,因为您可以使用一些循环和大量迭代快速对自己进行基准测试。 请参阅下面的快速示例,其中(在我的机器上)+操作符约为7秒,而strfmt约为10秒。 正如Guido Preite已经指出的那样,为了最好的可读性而写它,但通常在AX中使用strfmt来构建面向用户的字符串(例如通过语言)。 static void StrConcatPerf(Args _args) { int i; int startTime; int endTime; str conca ...
  • 提供此模板: #include template std::string Str( const TYPE & t ) { std::ostringstream os; os << t; return os.str(); } 然后你可以说: return getName() + "\t" + Str( Damage ) + "\t" + Str(Cost); 请注意,这几乎等同于Boost的lexical_cast ,以及即将推出的标 ...
  • ++看起来像增量运算符。 它实际上是两个一元+运算符,所以它和普通的旧counter ++ just looks like the increment operator. It's actually two unary + operators, so it's just the same as plain old counter
  • 字符串没有附加运算符。 字符串是不变的值。 ..运算符连接两个字符串,产生第三个字符串作为结果: local b = "con" local c = "catenate" local a = b .. c -- "concatenate" table.concat函数连接表中的字符串,产生一个字符串结果: local t = { "con", "catenate" } local a = table.concat(t) -- "concatenate" local t = { "two", "wor ...
  • 不,它不必有任何锁定结构。 任何纯粹的方法在多线程环境中都能正常工作。 “纯”我的意思是一种方法: 该函数总是在给定相同参数值的情况下评估相同的结果值。 函数结果值不能取决于任何隐藏的信息或状态,这些信息或状态可能随着程序执行的进行或程序的不同执行而改变,也可能取决于来自I / O设备的任何外部输入。 对结果的评估不会导致任何语义上可观察的副作用或输出,例如突变可变对象或输出到I / O设备。 (来源: 维基百科:纯功能 ) 在你的情况下,字符串连接本身在多线程环境中很好,并且不需要任何锁定结构。 但是,如 ...
  • 这个. 运算符是连接运算符。 您的第一个示例仅适用于echo函数(技术上它是一种语言结构,但不允许分割头发)接受多个参数,并且会打印每个参数。 因此,第一个示例是使用多个参数调用echo,并且它们都被打印出来,而第二个示例中所有字符串都是concatenate的,并且正在打印一个大字符串。 The . operator is the concatenation operator. Your first example only works because the echo 'function' (techn ...
  • 斜线正在逃避报价。 基本上它会在括号内的引号中写出名称。 对于您的示例,输出将是: items["Wilson"] = Wilson
    在反斜杠\之后直接对字符进行转义。 所以\"逃脱到了" 。 以下"正在停止字符串,您将附加s变量,而s变量又是foreach循环中的当前项。 这是一篇关于转义字符的文章 。 The slash is escaping the quote. Basically it is going to write the name in quotes inside brack ...