0

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

Name           | Email               | BDate
---------------------------------------------------------
John Doe       | jdoe@company.com    | June 1, 1980
Kelly Smith    |                     | June 2, 1983
Richmond White | rwhite@company.com  | 
John Doe       |                     | 
Kelly Smith    | ksmith@company.com  | 
Richmond White |                     | June 3, 1984

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

Name           | Email               | BDate
---------------------------------------------------------
John Doe       | jdoe@company.com    | June 1, 1980
Kelly Smith    | ksmith@company.com  | June 2, 1983
Richmond White | rwhite@company.com  | June 3, 1984

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

提前致谢!

4

2 回答 2

2

询问:

SQLFIDDLE示例

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

结果:

|           NAME |              EMAIL |        BDATE |
------------------------------------------------------
|       John Doe |   jdoe@company.com | June 1, 1980 |
|    Kelly Smith | ksmith@company.com | June 2, 1983 |
| Richmond White | rwhite@company.com | June 3, 1984 |
于 2013-06-13T07:55:28.387 回答
0

一种选择是使用以下查询创建一个新表,然后删除旧表:

SELECT Name, GROUP_CONCAT(Email) AS Email, GROUP_CONCAT(BDate) AS DBate
FROM YourTable
GROUP BY Name
于 2013-06-13T07:39:51.670 回答