我有一个包含三列的MySQL表:

Name           | Email               | BDate
---------------------------------------------------------
John Doe       | [email protected]    | June 1, 1980
Kelly Smith    |                     | June 2, 1983
Richmond White | [email protected]  |
John Doe       |                     |
Kelly Smith    | [email protected]  |
Richmond White |                     | June 3, 1984


我想要做的是删除重复的名称并合并单元格值。我希望我的桌子看起来像这样:

Name           | Email               | BDate
---------------------------------------------------------
John Doe       | [email protected]    | June 1, 1980
Kelly Smith    | [email protected]  | June 2, 1983
Richmond White | [email protected]  | June 3, 1984


我的查询将如何返回我的欲望表?

提前致谢!

最佳答案

查询:

SQLFIDDLEExample

SELECT t1.Name,
       MAX(t1.Email) Email,
       MAX(t1.BDate) BDate
FROM Table1 t1
GROUP BY t1.Name


结果:

|           NAME |              EMAIL |        BDATE |
------------------------------------------------------
|       John Doe |   [email protected] | June 1, 1980 |
|    Kelly Smith | [email protected] | June 2, 1983 |
| Richmond White | [email protected] | June 3, 1984 |

10-07 18:06