【问题标题】:Insert distinct records of one table to another into MySQL将一个表的不同记录插入另一个表到 MySQL
【发布时间】:2016-08-11 18:17:21
【问题描述】:

表 1:mydata_table
多列。但我想获取 3 列的不同记录。

Email_Office |电子邮件_个人1 | Email_Personal2

表 2:unique_emails

只有 3 列。邮箱_办公 |电子邮件_个人1 | Email_Personal2

我对表 2 的所有三列都尝试了此代码,以便从每列中插入不同的记录。

insert into unique_emails 
(email_personal1) 
select distinct email_personal1
from mydata_table
where Email_Personal1!=""

它会插入记录,但问题是如果 email_office 列中有 100 行,email_personal1 中有 300 行,那么它将显示前 100 行 email_office,其余两列为空,然后从第 101 行开始,它将显示 email_personal1 的记录剩下的两列是空的。我想将所有行插入在一起。我该怎么做?

【问题讨论】:

  • 顺便说一句:这是一个很好的例子,说明如果你忽略 1NF 会发生什么。

标签: mysql sql distinct


【解决方案1】:

尝试为需要唯一的列创建唯一索引,并使用@Sergey 提出的第一个SQL with ignore:

INSERT IGNORE INTO
  unique_emails 
  (email_office, email_personal1, email_personal2) 
  SELECT DISTINCT
    email_office,
    email_personal1,
    email_personal2
  FROM
    mydata_table
  WHERE
    email_office!=""
    OR Email_Personal1!=""
    OR Email_Personal2!=""

【讨论】:

  • 在这种情况下,你会得到比你需要的更多的行。例如,在我的回答中,您得到的结果是 4 行而不是 3
【解决方案2】:

如果你想插入唯一的三组电子邮件,你应该使用

insert into unique_emails 
(email_office, email_personal1, email_personal2) 
select distinct email_office, email_personal1, email_personal2
from mydata_table
where email_office!="" OR Email_Personal1!="" OR Email_Personal2!=""

但在这种情况下,某些电子邮件可能会在 unique_emails 表中出现多次。

假设表 1 包含记录:

Email_Office | Email_Personal1 | Email_Personal2
------------------------------------------------
a@a.com        b@b.com           c@c.com
a@a.com        b@b.com           c@c.com
a@a.com        b@b.com           e@e.com
a@a.com        c@c.com           NULL
a@a.com        d@d.com           e@e.com

然后结果表2

 Email_Office | Email_Personal1 | Email_Personal2   
------------------------------------------------
a@a.com        b@b.com           c@c.com
               c@c.com           e@e.com
               d@d.com

UPD。 试试这个:(SQL Fiddle)

insert into unique_emails 
(email_office, email_personal1, email_personal2)
select b.email_office, c.email_personal1, d.email_personal2
from (
  select @i := @i + 1 AS pos from mydata_table, (select @i := 0) r) a
left join (
  select t.*, @j := @j + 1 AS pos
  from (select distinct email_office from mydata_table where email_office!="") t,
    (select @j := 0) r) b on b.pos = a.pos
left join (
  select t.*, @k := @k + 1 AS pos
  from (select distinct email_personal1 from mydata_table where email_personal1!="") t,
    (select @k := 0) r) c on c.pos = a.pos
left join (
  select t.*, @l := @l + 1 AS pos
  from (select distinct email_personal2 from mydata_table where email_personal2!="") t,
    (select @l := 0) r) d on d.pos = a.pos
where b.email_office is not null or c.email_personal1 is not null or d.email_personal2 is not null

【讨论】:

  • 我预计表 2 中的 email_office、email_personal1 和 email_personal2 的唯一记录。每个记录都应该具有表 1 中的唯一记录。
  • 这对我来说仍然毫无意义。请为我的示例提供表 2 的确切预期内容。
  • 我在您的回答中编辑了结果表 2。看看吧。
猜你喜欢
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 2017-08-01
  • 2011-12-22
相关资源
最近更新 更多