2022-12-07:删除重复的电子邮箱。删除重复数据后,id=3的数据被删除。请问sql语句如何写?
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
`id` int(11) NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `person` VALUES ('1', 'john@example.com');
INSERT INTO `person` VALUES ('2', 'bob@example.com');
INSERT INTO `person` VALUES ('3', 'john@example.com');
答案2022-12-07:
sql语句如下:
DELETE p1 FROM person p1,
person p2
WHERE
p1.email = p2.email AND p1.id > p2.id
运行结果如下: