插入一列DISTINCT,以及从一个表到另一个表的相应其他列

时间:2017-05-15 21:50:18

标签: mysql database

我的数据库查询有问题。我有三个表项目,开发人员和电子邮件。在开发人员表中,有很多行具有相同的名称但不同的电子邮件。我必须在电子邮件表中插入不同的名称,但所有电子邮件(在他们所属的名称行中),即

    example
    /////////////////////////////////////////////
     developers table have records:-
     id_developer  project_id  name  email
          0            1       umar  umar@gmail.com
          1            1       umar  umar@developers.com

     Now i want to inert the data in email table as:-
     user_id    name      email_ids
        0       umar      umar@gmail.com
                          umar@developers.com
    ////////////////////////////////////////////

    projects
    ----------
    id_project
    name
    ----------

    developers
    ----------
    id_developer
    project_id
    name
    email
    ----------

    email
    ----------
    user_id
    name
    email_ids
    ----------

以下是我当前的查询。请帮我。提前致谢

    INSERT INTO email(user_id, dev_name, email_ids) 
    SELECT p.id_project, 
           d.name, 
           d.email 
      FROM projects p 
            INNER JOIN developers AS d 
            ON p.id_project = d.project_id 
     WHERE d.name IN (SELECT name 
                        FROM developers 
                       GROUP BY name HAVING(COUNT(name) > 1 )) 
     GROUP BY(d.name)

1 个答案:

答案 0 :(得分:1)

在评论中进行了一些对话后,您真正需要的是在此进行正确的数据建模。

以数据库的方式存储数据是一种非常糟糕的做法。

  user_id    name      email_ids
    0       umar      umar@gmail.com
                      umar@developers.com

您将在将来遇到问题时检索此数据,因为您必须找到一种方法,以便在您需要时检索或拆分此数据。

因此,根据您当前的模型来满足您的要求,您只需稍微更改表格电子邮件即可。你的模型就是这样:

projects       developers        email
----------     -------------     ------------
id_project     id_developer      id
name           project_id        id_developer
               name              email
----------     -------------     ------------

因此,由于您已经拥有developers表中的数据,因此首先删除表格表格,然后以正确的方式重新创建表格。您需要执行:

drop table email;
create table dev_email( -- changed the name because there is a field with same name
    id INTEGER AUTO_INCREMENT NOT NULL,
    id_developer INTEGER NOT NULL, -- this column should be the same type 
                                   -- as id_developer in the table developers
    email VARCHAR(150) NOT NULL
    PRIMARY KEY pk_email (id),
    CONSTRAINT uk_developer_email UNIQUE (id_developer, email), -- that will avoid duplicates,
    CONSTRAINT fk_dev FOREIGN KEY (developer_id) 
         REFERENCES developers(id_developer)
          ON UPDATE RESTRICT ON DELETE RESTRICT
);

现在让我们用正确的数据填充此表:

INSERT INTO dev_email (id_developer, email)
   SELECT min(id_developer), email
     FROM developers
    GROUP BY email;

之后我们必须删除developers表中的重复数据,如下所示:

DELETE FROM developers d
  WHERE NOT EXIST (SELECT 1 
                     FROM dev_email de 
                    WHERE de.id_developer = d.id_developer);

然后我们删除developers表中不再需要的列:

ALTER TABLE developers DROP COLUMN email;

这应该为您提供适当的标准化模型。

现在,如果您需要检索连接所有电子邮件的开发人员(这比分割它更简单),您只需执行以下操作:

 SELECT d.id_developer, 
        d.name,
        GROUP_CONCAT(e.email, ', ') as emails
   FROM developers d
            INNER JOIN dev_email e
               ON d.id_developer = e.id_developer
  GROUP BY d.id_developer, 
           d.name

PS:我完成了所有这些工作,请先在测试环境中运行(当前数据库的副本是安全的)。它应该没问题,但比抱歉更安全吗?