字符串函数
1、concat 函数
drop table test;
create table test(id int(4), name varchar(10), sex char(2));
insert into test values(1, 'Tom', '男');
select concat(id, name, sex) from test; //查询结果:1Tom男
select concat(id, '-', name, sex) from test; //查询结果:1-Tom男
update test set sex = null;
select concat(id, name, sex) from test; //结果为null, 有一个为null, 结果为null
2、concat_ws 函数
select concat_ws('-', id, name, sex) from test; //结果为1-Tom
第一个参数是其它参数的分隔符;即使有一个结果为null,其它的不为null,结果就不会为null
3、group_concat 函数
drop table test;
create table test (name varchar(10), id int(4));
insert into test values('天',1),('道',2),('酬',3),('勤',4);
insert into test values('天',1),('道',2),('酬',3),('勤',4);
select id, group_concat(name) from test group by id; // 默认逗号分隔
1天,天
2道,道
3酬,酬
4勤,勤
select id, group_concat(name, '_') from test group by id;
1天_,天_
2道_,道_
3酬_,酬_
4勤_,勤_
select id, group_concat(name separator'_') from test group by id;
1天_天
2道_道
3酬_酬
4勤_勤
select id, group_concat(distinct name order by name desc separator '_') from test group by id;
1天
2道
3酬
4勤
分组函数
select cate_name as catename,count(cate_name) as count from `intl_order_goods` where order_id = 1 group by cate_name
执行结果: