mysql 函数group_concat 和find_in_set

idscore
155
222
225
333
334
446
448

group_concat

将多行数据按照某个条件合并成一行

select id,group_concat(score) from table group by id;

155
222,25
333,34
446,48

dinstinct去重, separator ';'表示以分号为分隔符

select id,group_concat(distinct score separator ‘;’) from table group by id;

155
222;25
333;34
446;48

find_in_set

将一个字段的值拆分成多行,字段值需要以逗号隔开

String name=“A,B,C,D”;
select * from table where find_in_set(id,name);
表示求表table中id值为A,B,C,D的数据,跟下面的sql一个含义
select * from table where id in('A','B','C','D');

可以用于码值配置
select * from table where find_in_set(id,(select itemno from code_library where codeno='A'));

FIND_IN_SET(str,strlist) : str 要查询的字符串,strlist 需查询的字段,参数以”,”分隔,该函数的作用是查询字段(strlist)中是否包含(str)的结果.

SELECT find_in_set(‘B’, ‘A,B,C,D’) from dual;
结果:2 B在strlist集合中2的位置
select find_in_set(‘A’, ‘A,B,C,D’) from dual;
结果:1 A在strlist集合中1的位置
select find_in_set(‘A’, ‘B,C,D’) from dual;
结果:0 strlist中不存在str,所以返回0。

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值