简体   繁体   English

删除mysql group_concat选定的行

[英]removing mysql group_concat selected rows

Although this question was asked two years ago the solutions did not use group_concat. 尽管此问题是两年前提出的,但解决方案并未使用group_concat。 so keeping it brief given a table 所以给它一个简短的表

id    name
1     Tim
2     Sam
3     Bob
4     Joe
5     Ali
6     Kay
7     Bob
8     Joe

running the following 运行以下

select name, count(*) c, group_concat(id) rows
  from table
 group by name
having c > 1;

gives

name   c   rows
Bob    2   3,7
Joe    2   4,8

is is possible to then remove rows 3,7,4 and 8 from my table without writing a script? 然后可以在不编写脚本的情况下从表中删除第3、7、4和8行? I think I do need to use group_concat because in reality my "name" column is a concatenation of three columns in the table. 我认为我确实需要使用group_concat,因为实际上我的“名称”列是表中三列的串联。

SELECT count(`access`.id) as 
extra_access,`access`.perm_role_id,group_concat(temp.extra_id) as extra_ids
FROM `access`  
INNER JOIN(  
SELECT `access`.id,perm_role_id,perm_module_id,group_concat(id) as extra_id
FROM `access`
GROUP BY perm_role_id,perm_module_id     
HAVING COUNT(id) =1  
)temp ON access.id= temp.id GROUP BY perm_role_id

Use join again and concat again for this 再次使用join并再次连接

If you want to remove the duplicate row, try this 如果要删除重复的行,请尝试此操作

DELETE from table where id NOT IN (select id from table GROUP BY name having count(*) = 1)

delete all rows which are not unique 删除所有不唯一的行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM