gpt4 book ai didi

mysql - 使用合并后尝试删除重复的数据行

转载 作者:太空宇宙 更新时间:2023-11-03 10:51:48 25 4
gpt4 key购买 nike

我有一个表,其中针对我的用户拥有的每种访问类型都有一条记录:

cm812    BRTYF78
cm812 BRgh5VD
cm812 GHYF7GJ

我想查询表并得到:

cm812    BRTYF78,BRgh5VD,GHYF7GJ

这是我写的:

  SELECT id
,COALESCE(access+',' ,'') + (access) as access
FROM [accesslist]
group by id, access

我得到的是:

cm812      BRTYF78,BRgh5VD,GHYF7GJ

cm812 BRTYF78,BRgh5VD,GHYF7GJ

cm812 BRTYF78,BRgh5VD,GHYF7GJ

如何获得一个条目???:

cm812      BRTYF78,BRgh5VD,GHYF7GJ

最佳答案

SQL Server Fiddle :

SELECT A.id, 
SUBSTRING((
SELECT ', ' + B.access
FROM accesslist B
WHERE B.id = A.id
ORDER BY access
For XML Path('')),2,8000) AS AccessList
FROM accesslist A
GROUP BY A.id

MySQL Fiddle :

SELECT A.id, 
GROUP_CONCAT(access ORDER BY access SEPARATOR ', ') AS AccessList
FROM accesslist A
GROUP BY A.id

关于mysql - 使用合并后尝试删除重复的数据行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24291597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com