我的mysql数据库中有这个表
+------------+---------+
| Age | Name |
+------------+---------+
| 30 | A |
+------------+---------+
| 24 | B |
+------------+---------+
| 30 | C |
+------------+---------+
| 24 | D |
+------------+---------+
我希望像这样显示新表
+------------+---------+
| Age | Name |
+------------+---------+
| 30 | A |
+ +---------+
| | C |
+------------+---------+
| 24 | B |
+ +---------+
| | D |
+------------+---------+
任何人都知道如何解决这个问题。谢谢你提前帮忙。这是代码,它会一直显示重复的年龄。如果不够清楚,请对不起。
SELECT DISTINCT(dupe.age),name
FROM customer
INNER JOIN
( SELECT age
FROM customer
GROUP BY age
HAVING COUNT(*) > 1
) dupe
ON dupe.age = customer.age
答案 0 :(得分:4)
如何使用GROUP_CONCAT
代替:
SELECT
age,
GROUP_CONCAT(DISTINCT name)
FROM
customer
GROUP BY age
HAVING COUNT(*) > 1
的 The SQLFIDDLE. 强> 的
答案 1 :(得分:1)
SELECT DISTINCT(dupe.age),name
FROM customer
INNER JOIN
( SELECT age
FROM customer
GROUP BY age
HAVING COUNT(*) > 1
) dupe
ON dupe.age = customer.age
ORDER BY age DESC, name
如果您只想显示一次不同的年龄(根据您的图表),请参阅@xdazz的答案。具体来说,您想要4行,其中2行的年龄为null,或者您想要2行,每行有两个名称。如果是后者,请参阅@xdazz的答案。