我所做的是创建一个名为 Rent2 的临时表来存放我的客户注册的所有重复模块,然后在删除临时表之前执行删除内部连接语句。
但是完成后总会有一个剩余的副本。我在这里做错了什么?
CREATE TABLE IF NOT EXISTS `tblRent2` (
`IdRent` INT(11) NOT NULL)
ENGINE = InnoDB;
INSERT INTO tblRent2 (SELECT IdRent FROM tblRent WHERE IdModule = 1 GROUP BY IdClient HAVING COUNT(IdModule) > 1);
INSERT INTO tblRent2 (SELECT IdRent FROM tblRent WHERE IdModule = 2 GROUP BY IdClient HAVING COUNT(IdModule) > 1);
INSERT INTO tblRent2 (SELECT IdRent FROM tblRent WHERE IdModule = 3 GROUP BY IdClient HAVING COUNT(IdModule) > 1);
INSERT INTO tblRent2 (SELECT IdRent FROM tblRent WHERE IdModule = 4 GROUP BY IdClient HAVING COUNT(IdModule) > 1);
INSERT INTO tblRent2 (SELECT IdRent FROM tblRent WHERE IdModule = 5 GROUP BY IdClient HAVING COUNT(IdModule) > 1);
INSERT INTO tblRent2 (SELECT IdRent FROM tblRent WHERE IdModule = 6 GROUP BY IdClient HAVING COUNT(IdModule) > 1);
INSERT INTO tblRent2 (SELECT IdRent FROM tblRent WHERE IdModule = 999 GROUP BY IdClient HAVING COUNT(IdModule) > 1);
DELETE r.* FROM tblRent r INNER JOIN tblRent2 r2 ON
r.idRent = r2.idRent;
SELECT * FROM tblRent2;
DROP TABLE tblRent2;
原始表如下所示:
IdRent | IDClient | IdModule
1 | 30 | 999
2 | 30 | 999
3 | 31 | 1
4 | 31 | 1
这些都是主键。