8

问题

我有 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                                                                |
+----------+----------------------------------------------+---------------------------------------------------------------------+
4

3 回答 3

1

如果我理解正确,您还需要将其他字段与电子邮件相关联(例如:类型)。

使用 group_concat 您可以将电子邮件类型添加到结果中,在我的示例中为“;” 将邮件类型分开,“,”将结果分开。

SELECT u.name,  
    GROUP_CONCAT( distinct p.phone),  
    GROUP_CONCAT( distinct 'mail=',e.mail,';type=' ,type)  
    FROM people u
    LEFT OUTER JOIN phone p ON u.id=p.id
    LEFT OUTER JOIN mail e ON u.id=e.id
    GROUP BY u.id
于 2012-11-23T20:41:24.730 回答
1

你想要的根本不是你想要的,如果这有任何意义的话......你实际上不能用你的数据库输出以编程方式做任何事情,你需要用它做一些事情(除非你只是在运行一个查询直接在您的数据库上)。

既然您说“我也会对 PHP 解决方案感到满意”。...您真正想要的是类似于 PHP“用户”对象的东西,例如(这当然是假设的):

<?php

class User {

  private $_id;
  private $_telNos = array();
  private $_emails = array();

  public function __construct($iUserId = null, $oDatabaseAbstractionObject = null) {
    if(!is_null($iUserId)) $this->setId($iUserId);
    if(!is_null($oDatabaseConnectionObject)) $this->load($iUserId, $oDatabaseAbstractionObject);
  }

  public setId($iUserId) {
    $this->_id = (int) $iUserId;
  }

  public getId() {
    return $this->_id;
  }

  /* telephone and email setters and getters */

  public function load($iUserId, $oDatabaseAbstractionObject) {
    /* error trapping - for example if $iUserId is null */

    $this->setTelNos($oDatabaseAbstractionObject->readTelNos($iUserId));
    $this->setEmails($oDatabaseAbstractionObject->readEmails(iUserId));
  }
}

?>

然后,您的数据库抽象对象只需要执行一些非常简单的查询来读取您的用户、用户电子邮件和电话号码表,并将结果作为数组返回,然后您可以将其直接放入您的 PHP 对象中。例如:

<?php

/**
 * this implements a database connection object as a private class member
 */
class DBUser {

  private $_conn;

  /* constructor other functionality */

  /**
   * method to pass an SQL query to the database and return an array of results
   */
  public function readTelNos($iUserId) {
    return $this->_conn->read("SELECT `number` from `tel` WHERE `user_id` = " . (int) $iUserId);
  }

}
?>

这将您的问题分解为更小、更容易处理的问题,并将它们全部包装在漂亮的 PHP 对象中,然后您可以实际使用这些对象。

您的用户对象将有一个列表,如果电话号码和电子邮件可以通过类似的方式轻松检索,$oUser->getEmails();并且如果它们存储为对象内的关联数组,您甚至可以通过“标签”检索它们$oUser->getEmail('work');

于 2012-11-23T17:02:01.627 回答
0

首先创建临时表名称 temp_tb 而不是插入记录。然后使用 Row_Number() 进行分区并删除多次列出的所有记录。就像受到打击一样。

    Insert into temp_tb 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;
   SELECT * from temp_tb 
    With A as
  (
 select People.Name, People.ID, Phones.Number, Emails.Email ROW_NUMBER() 
  OVER (Partition by ID ORDER BY Fee_temp.Name) As Number from temp_tb 
 )
 DELETE FROM A WHERE Number>1
 SELECT * FROM temp_tb 

希望它可以帮助你。

于 2014-06-20T12:30:23.443 回答