单个表的内部联接 - mysql

时间:2013-12-17 18:54:40

标签: mysql

我有下表

教练(coach_code,coach_name,year_of_birth)

我需要创建一个查询,返回在同一年和同一年出生的教练对。每对应该只出现一次。 (教练1,教练2,年份_b)

问题在于它是同一个表而且内连接不起作用。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

SELECT c1.coach_code AS coach1, c2.coach_code AS coach2, c1.year_of_birth
FROM coaches AS c1
JOIN coaches AS c2 ON c1.year_of_birth = c2.year_of_birth AND c1.coach_code < c2.coach_code

答案 1 :(得分:1)

您可以使用GROUP_CONCAT但请注意GROUP_CONCAT中的字符数限制

SELECT GROUP_CONCAT(coach_name SEPARATOR ',') as `pair`,year_of_birth
FROM coaches
GROUP BY year_of_birth

GROUP_CONCAT(expr)