问题
我有 3 个表:人员、电话和电子邮件。每个人都有一个唯一的 ID,每个人可以有多个号码或多个电子邮件。
简化后看起来像这样:
+---------+----------+
| ID | Name |
+---------+----------+
| 5000003 | Amy |
| 5000004 | George |
| 5000005 | John |
| 5000008 | Steven |
| 8000009 | Ashley |
+---------+----------+
+---------+-----------------+
| ID | Number |
+---------+-----------------+
| 5000005 | 5551234 |
| 5000005 | 5154324 |
| 5000008 | 2487312 |
| 8000009 | 7134584 |
| 5000008 | 8451384 |
+---------+-----------------+
+---------+------------------------------+
| ID | Email |
+---------+------------------------------+
| 5000005 | Smithley@goodmail.com.com |
| 5000005 | Smithley.j@gmail.com |
| 5000008 | Smithley@gmail.com |
| 5000008 | tech@goodmail.com |
| 5000008 | feler@campus.uni.com |
| 8000009 | Ashley.hill86@gmail.com |
| 5000004 | georgestanko@hotmail.com |
+---------+------------------------------+
我正在尝试将它们连接在一起而不会重复。当我尝试只加入与人的电子邮件或仅与人的电话时,它的效果很好。
SELECT People.Name, People.ID, Phones.Number
FROM People
LEFT OUTER JOIN Phones ON People.ID=Phones.ID
ORDER BY Name, ID, Number;
+----------+---------+-----------------+
| Name | ID | Number |
+----------+---------+-----------------+
| Steven | 5000008 | 8451384 |
| Steven | 5000008 | 24887312 |
| John | 5000005 | 5551234 |
| John | 5000005 | 5154324 |
| George | 5000004 | NULL |
| Ashley | 8000009 | 7134584 |
| Amy | 5000003 | NULL |
+----------+---------+-----------------+
SELECT People.Name, People.ID, Emails.Email
FROM People
LEFT OUTER JOIN Emails ON People.ID=Emails.ID
ORDER BY Name, ID, Email;
+----------+---------+------------------------------+
| Name | ID | Email |
+----------+---------+------------------------------+
| Steven | 5000008 | Smithley@gmail.com |
| Steven | 5000008 | tech@goodmail.com |
| Steven | 5000008 | feler@campus.uni.com |
| John | 5000005 | Smithley@goodmail.com.com |
| John | 5000005 | Smithley.j@gmail.com |
| George | 5000004 | georgestanko@hotmail.com |
| Ashley | 8000009 | Ashley.hill86@gmail.com |
| Amy | 5000003 | NULL |
+----------+---------+------------------------------+
但是,当我尝试在 People 上加入电子邮件和电话时 - 我得到了这个:
SELECT People.Name, People.ID, Phones.Number, Emails.Email
FROM People
LEFT OUTER JOIN Phones ON People.ID = Phones.ID
LEFT OUTER JOIN Emails ON People.ID = Emails.ID
ORDER BY Name, ID, Number, Email;
+----------+---------+-----------------+------------------------------+
| Name | ID | Number | Email |
+----------+---------+-----------------+------------------------------+
| Steven | 5000008 | 8451384 | feler@campus.uni.com |
| Steven | 5000008 | 8451384 | Smithley@gmail.com |
| Steven | 5000008 | 8451384 | tech@goodmail.com |
| Steven | 5000008 | 24887312 | feler@campus.uni.com |
| Steven | 5000008 | 24887312 | Smithley@gmail.com |
| Steven | 5000008 | 24887312 | tech@goodmail.com |
| John | 5000005 | 5551234 | Smithley@goodmail.com |
| John | 5000005 | 5551234 | Smithley.j@gmail.com |
| John | 5000005 | 5154324 | Smithley@goodmail.com |
| John | 5000005 | 5154324 | Smithley.j@gmail.com |
| George | 5000004 | NULL | georgestanko@hotmail.com |
| Ashley | 8000009 | 7134584 | Ashley.hill86@gmail.com |
| Amy | 5000003 | NULL | NULL |
+----------+---------+-----------------+------------------------------+
会发生什么 - 如果一个人有 2 个号码,他的所有电子邮件都会显示两次(它们无法排序!这意味着它们不能被 @last 删除)
我想要的是:
底线,与@last一起玩,我想以这样的方式结束,但如果我不以正确的方式排列 ORDER 列,@last 将不起作用 - 这似乎是一个大问题..Orderin the电子邮件栏。因为从上面的例子看:
Steven 有 2 个电话号码和 3 个电子邮件。每封电子邮件都会出现带有数字的 JOIN 电子邮件 - 因此无法排序的重复值(SORT BY 不适用于它们)。
**THIS IS WHAT I WANT**
+----------+---------+-----------------+------------------------------+
| Name | ID | Number | Email |
+----------+---------+-----------------+------------------------------+
| Steven | 5000008 | 8451384 | feler@campus.uni.com |
| | | 24887312 | Smithley@gmail.com |
| | | | tech@goodmail.com |
| John | 5000005 | 5551234 | Smithley@goodmail.com |
| | | 5154324 | Smithley.j@gmail.com |
| George | 5000004 | NULL | georgestanko@hotmail.com |
| Ashley | 8000009 | 7134584 | Ashley.hill86@gmail.com |
| Amy | 5000003 | NULL | NULL |
+----------+---------+-----------------+------------------------------+
现在有人告诉我,最好将电子邮件和号码放在分开的表格中,因为一个人可以有很多电子邮件。那么,如果这是一件很常见的事情,那么有什么不简单的解决方案呢?
我也会对 PHP 解决方案感到满意。
我现在知道该怎么做,满足它,但不那么漂亮。
如果我使用 GROUP_CONTACT 执行此操作,我会得到令人满意的结果,但它看起来并不那么漂亮:我不能在它旁边放置“电子邮件类型 = 工作”。
SELECT People.Ime,
GROUP_CONCAT(DISTINCT Phones.Number),
GROUP_CONCAT(DISTINCT Emails.Email)
FROM People
LEFT OUTER JOIN Phones ON People.ID=Phones.ID
LEFT OUTER JOIN Emails ON People.ID=Emails.ID
GROUP BY Name;
+----------+----------------------------------------------+---------------------------------------------------------------------+
| Name | GROUP_CONCAT(DISTINCT Phones.Number) | GROUP_CONCAT(DISTINCT Emails.Email) |
+----------+----------------------------------------------+---------------------------------------------------------------------+
| Steven | 8451384,24887312 | Smithley@gmail.com,tech@goodmail.com,feler@campus.uni.com |
| John | 5551234,5154324 | Smithley@goodmail.com,Smithley.j@gmail.com |
| George | NULL | georgestanko@hotmail.com |
| Ashley | 7134584 | Ashley.hill86@gmail.com |
| Amy | NULL | NULL |
+----------+----------------------------------------------+---------------------------------------------------------------------+