怎么把select查出来的值,如果字段A相等,把字段B合并? 比如用select name,cellphone from table1查出来下面的记录
姓名 电话
王三 111111
王三 222222
张四 333333
那么sql怎么改,可以出来如下的结果?
姓名 电话
王三 111111;222222
张四 333333
------解决方案--------------------
SELECT 'ws',cellphone,(SELECT cellphone FROM (SELECT ROWNUM num,cellphone FROM table1 WHERE NAME='ws') a WHERE a.num=2
) cellphone FROM table1 WHERE NAME='ws' AND ROWNUM=1
UNION SELECT 'ls',cellphone,0 FROM table1 WHERE NAME='ls'
随手写的 ------解决方案--------------------
select name,wm_concat(cellphone) phone from table1 group by name
------解决方案-------------------- WITH TEST AS (
SELECT 'ZS' AS XM,'111111' AS DH FROM DUAL
UNION ALL
SELECT 'ZS' AS XM,'222222' AS DH FROM DUAL
UNION ALL
SELECT 'LS' AS XM,'333333' AS DH FROM DUAL
)
SELECT XM,LISTAGG(DH,';')WITHIN GROUP(ORDER BY DH) AS DH FROM TEST GROUP BY XM
=============================================
1 LS 333333
2 ZS 111111;222222 ------解决方案--------------------
SELECT XM,REPLACE(wm_concat(DH),',',';')
FROM table
GROUP BY XM;