显示同一个生日MySQL的名称

时间:2014-09-21 04:22:52

标签: mysql select

我正在尝试查询显示同一天出生的音乐家

我有下表

为person_id
familyName
给定名称
生日

这是我目前的代码

SELECT P.birthDate, DATE_FORMAT(birthDate,'%d %b %Y'), CONCAT(familyName, " ",givenName)
AS Person
FROM person P
WHERE givenName IS NOT NULL
AND familyName IS NOT NULL
AND birthDate IS NOT NULL
AND DATE_FORMAT(birthDate,'%d %b %Y') IS NOT NULL
ORDER BY DATE_FORMAT(birthDate,'%d %b %Y');

我似乎无法弄清楚如何使用where或其他子句来执行此操作。

2 个答案:

答案 0 :(得分:0)

什么是分组依据

GROUP BY语句与聚合函数结合使用,可以按一列或多列对结果集进行分组。

SQL GROUP BY语法

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

在您的情况下,它会将所有具有相同出生日期的音乐人分组,其中出生日期将是您的专栏,存储每位音乐家的出生日期。希望它有所帮助...

答案 1 :(得分:0)

有些选择:

1)分组

SELECT P.birthDate, DATE_FORMAT(birthDate,'%d %b %Y'), GROUP_CONCAT(CONCAT(familyName, " ",givenName)) AS PersonNames
  FROM person P
 WHERE concat( givenName, familyName, birthDate) IS NOT NULL
 GROUP BY P.birthDate;

2)加入

SELECT P.person_id, P.birthDate, 
       DATE_FORMAT(P.birthDate,'%d %b %Y'), 
       CONCAT(P.familyName, " ",P.givenName) AS PersonName, 
       CONCAT(PB.familyName, " ",PB.givenName) AS OtherPersonName
  FROM person P
  LEFT JOIN person PB
    ON P.birthDate = PB.birthDate
 WHERE concat( P.givenName, P.familyName, P.birthDate) IS NOT NULL
   AND concat( PB.givenName, PB.familyName ) IS NOT NULL
 ORDER BY P.person_id,PB.person_id;