当前位置:首页 > 数据库 > 正文

mysql指定字符位置截取字符串(多行重复信息进行合并)

2024-03-31 数据库

select number ,SUBSTRING_INDEX(GROUP_CONCAT(name ORDER BY name desc ),‘,‘,1),
SUBSTRING_INDEX(GROUP_CONCAT(idcard ORDER BY name desc ),‘,‘,1),
SUBSTRING_INDEX(GROUP_CONCAT(branch ORDER BY name desc ),‘,‘,1),
SUBSTRING_INDEX(GROUP_CONCAT(department ORDER BY name desc ),‘,‘,1),
SUBSTRING_INDEX(GROUP_CONCAT(direct_superior ORDER BY name desc ),‘,‘,1),
SUBSTRING_INDEX(GROUP_CONCAT(position ORDER BY name desc ),‘,‘,1),
SUBSTRING_INDEX(GROUP_CONCAT(location ORDER BY name desc ),‘,‘,1),
GROUP_CONCAT(datasources ORDER BY name desc )
from employee_information GROUP BY number

 

 

使用SUBSTRING_INDEX()函数
substring_index(str,delim,count)

str:要处理的字符串

delim:分隔符

count:分隔符计数 

例子
取出上述表中数组的第一个元素

-- SUBSTRING_INDEX(str,‘"‘,2)取出从左往右第二个引号左边的字符串str
-- SUBSTRING_INDEX(str1,‘"‘,-1)取出从右往左数第一个引号右边的字符串str_1

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/SQL/13041.html