用更高的值大量替换更多表中的值

时间:2010-06-14 18:09:23

标签: sql mysql

我有一个包含OWN_ID和OWN_Email的表 -

own_id      |   own_email
-----------------------------------------------
3ace7cf80edd    |   email@example.com
3acf6af33ff7    |   email@example.com
3acda2524e00    |   email@example.com
3ad75583c9a7    |   spam@example.com
3ad74b018999    |   spam@example.com

问题是它应该每个电子邮件只包含一个ID,我还需要用OWN_Email的最高OWN_ID值替换另一个表中的所有OWN_ID值

sql create:

CREATE TABLE  `blahblah.`eventowner` (
  `OWN_ID` varchar(12) COLLATE utf8_czech_ci NOT NULL DEFAULT '',
  `OWN_Email` varchar(120) COLLATE utf8_czech_ci DEFAULT NULL,
  `OwnDateFormat` varchar(16) COLLATE utf8_czech_ci DEFAULT NULL,
  `OwnWeekStart` int(11) DEFAULT NULL,
  `OwnDayStart` int(11) DEFAULT NULL,
  `OwnDayEnd` int(11) DEFAULT NULL,
  PRIMARY KEY (`OWN_ID`),
  KEY `OwnerEmailIndex` (`OWN_Email`),
  KEY `OwnerIndex` (`OWN_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci

CREATE TABLE  `blahblah`.`event` (
  `EVN_ID` varchar(128) COLLATE utf8_czech_ci DEFAULT NULL,
  `EVNGRP_ID` varchar(12) COLLATE utf8_czech_ci DEFAULT NULL,
  `EVNOWN_ID` varchar(12) COLLATE utf8_czech_ci DEFAULT NULL,
  `EVNRCR_ID` varchar(12) COLLATE utf8_czech_ci DEFAULT NULL,
  `Evn_EditCounter` int(11) DEFAULT NULL,
  `Evn_Created` int(11) DEFAULT NULL,
  `Evn_Modified` int(11) DEFAULT NULL,
  `EvnFolder` varchar(128) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnTitle` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnNote` text COLLATE utf8_czech_ci,
  `EvnLocation` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnPriority` int(11) DEFAULT NULL,
  `EvnComplete` int(11) DEFAULT NULL,
  `EvnColor` varchar(2) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnClass` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnShareType` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnTimeFormat` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnType` varchar(127) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnStatus` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnOrganizer` varchar(80) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnContact` varchar(80) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnURL` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnStartDate` int(11) DEFAULT NULL,
  `EvnStartTime` int(11) DEFAULT NULL,
  `EvnEndDate` int(11) DEFAULT NULL,
  `EvnEndTime` int(11) DEFAULT NULL,
  `EvnRID` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnUID` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
  `EvnExpire` int(11) DEFAULT NULL,
  `EvnSequence` int(11) DEFAULT NULL,
  `EvnFlags` int(11) DEFAULT NULL,
  KEY `EventGroupClassIndex` (`EVNGRP_ID`,`EvnClass`),
  KEY `EventGroupFolderIndex` (`EVN_ID`,`EVNGRP_ID`,`EvnFolder`),
  KEY `EventRIDIndex` (`EvnRID`),
  KEY `EventUIDIndex` (`EvnUID`),
  KEY `EventGroupRCRIndex` (`EVN_ID`,`EVNGRP_ID`,`EVNRCR_ID`),
  KEY `EventExpireIndex` (`EvnExpire`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci

2 个答案:

答案 0 :(得分:1)

使用核心整个表格的交易可以很轻松地完成这项工作。

BEGIN;
 SELECT own_email, max(own_Id) INTO temptable FROM table GROUP BY own_email
 TRUNCATE table;
 INSERT INTO table SELECT * FROM temptable;
 DELETE temptable;
COMMIT;

但是,这不是最好的方法 - 您也可以通过删除正确的行来实现。

DELETE FROM table AS d WHERE EXISTS (
  SELECT own_id FROM table AS t
  WHERE NOT EXISTS (
    SELECT max(own_id) FROM table AS i
    GROUP BY own_email
    WHERE t.own_email = i.own_email
  )
  AND d.own_id = t.own_id
  AND d.own_email = t.own_email
);

此处我们会删除所有own_id,而不是电子邮件的max(own_id)

这可以通过NOT IN完成(了解空值的警告),或者从

中删除表上的反连接

答案 1 :(得分:1)

从表t1,表t2中删除t1 其中t1.own_email = t2.own_email
和t1.own_id< t2.own_id

更新othertable,表
set othertable.own_id = table.own_id
其中othertable.own_email = table.own_email;