【发布时间】:2019-11-24 19:42:32
【问题描述】:
【问题讨论】:
-
MySQL 还是 MariaDB?它是哪一个?提供有关您的环境、场景和问题的实际、具体、有用的详细信息。 “它不起作用”是不可接受的。
-
考虑处理应用代码中数据显示的问题
【问题讨论】:
使用group_concat() 聚合id 并对表进行自连接:
select t.id, t.name,
group_concat(tt.id order by tt.id) repeated
from tablename t left join tablename tt
on tt.name = t.name and tt.id <> t.id
group by t.id, t.name
order by t.id
请参阅demo。
结果:
| id | name | repeated |
| --- | ----- | -------- |
| 1 | John | 4,8 |
| 2 | Smith | 3 |
| 3 | Smith | 2 |
| 4 | John | 1,8 |
| 5 | Anna | |
| 6 | David | 7 |
| 7 | David | 6 |
| 8 | John | 1,4 |
【讨论】: