concat()函数

1 MySQLconcat函数可以连接一个或者多个字符串,

select concat('10');//10

select concat('11','22','33');//112233

Oracleconcat函数只能连接两个字符串,不能多也不能少

select concat('11','22'from dual;

2 MySQLconcat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL

select concat('11','22',null);//null

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

select concat('11',NULLfrom dual;//11

concat_ws()函数
表示concat with separator,即有分隔符的字符串连接

select concat_ws(',','11','22','33');//11,22,33

select concat_ws('|','11','22','33');//11|22|33

select concat_ws('*','11','22',NULL);//11*225

concat不同的是, concat_ws函数在执行的时候,不会因为NULL值而返回NULL

group_concat()

可用来行转列

完整的语法如下:

group_concat([DISTINCT] 要连接的字段[Order BY ASC/DESC 排序字段] [Separator ‘分隔符’])

例子:

create table aa(

  id int,

  name VARCHAR(255)

);

insert  into aa values(1,10);

insert  into aa values(1,10);

insert  into aa values(1,20);

insert  into aa values(1,30);

insert  into aa values(3,30);

insert  into aa values(5,60);

insert  into aa values(5,90);

insert  into aa values(6,990);

id分组,把name字段的值打印在一行,逗号分隔(默认)

select id,group_concat(name) from aa group by id;

id分组,把name字段的值打印在一行,分号分隔

select id,group_concat(name separator ';'from aa group by id;

id分组,把去冗余的name字段的值打印在一行,逗号分隔


select id,group_concat(distinct name separator ';'from aa group by id;

id分组,把name字段的值打印在一行,*号分隔,name排倒序

select id,group_concat(name order by name desc separator "*"from aa group by id;

repeat()函数 
用来复制字符串,如下’ab’表示要复制的字符串,2表示复制的份数

select repeat('ab',2);//abab

select repeat('a',2);//aa