id | score |
---|---|
1 | 55 |
2 | 22 |
2 | 25 |
3 | 33 |
3 | 34 |
4 | 46 |
4 | 48 |
group_concat
将多行数据按照某个条件合并成一行
select id,group_concat(score) from table group by id;
1 | 55 |
---|---|
2 | 22,25 |
3 | 33,34 |
4 | 46,48 |
dinstinct去重, separator ';'表示以分号为分隔符
select id,group_concat(distinct score separator ‘;’) from table group by id;
1 | 55 |
---|---|
2 | 22;25 |
3 | 33;34 |
4 | 46;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。