【问题标题】:Deleting equal rows in mySQL 5.7.9?在 mySQL 5.7.9 中删除相等的行?
【发布时间】:2015-10-30 16:47:31
【问题描述】:

我在mysql中有这个表叫ts1

+----------+-------------+---------------+
| position | email       | date_of_birth |
+----------+-------------+---------------+
|        3 | NULL        | 1987-09-03    |
|        1 | NULL        | 1982-03-26    |
|        2 | Sam@gmail   | 1976-10-03    |
|        2 | Sam@gmail   | 1976-10-03    |
+----------+-------------+---------------+

我想使用 ALTER IGNORE 删除相等的行。

我试过了

ALTER IGNORE TABLE ts1 ADD UNIQUE INDEX inx (position, email, date_of_birth); 

ALTER IGNORE TABLE ts1 ADD UNIQUE(position, email, date_of_birth); 

在这两种情况下我都会得到 ​​p>

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IGNORE TABLE ts1 ADD UNIQUE(position, email, date_of_birth)' at line 1

我正在使用 mySQL 5.7.9。有什么建议吗?

【问题讨论】:

  • 好的,你能指出最有效的替代方案吗?
  • 可能需要添加 PK,因此当您手动删除它们(或通过一些保留 min() 策略)时,您有一行或多行要定位。然后当一切都清理干净后,将那个独特的复合材料放在那里
  • 创建一个带有 DISTINCT 结果的新索引表。
  • 是的,更容易,正如草莓所说,see pastie

标签: mysql


【解决方案1】:

要根据表格内联执行此操作,只需考虑您显示的列,请考虑以下内容。要按照 Strawberry 的建议在新表中执行此操作,请参阅 cmets 下的我的粘贴链接。

create table thing
(   position int not null,
    email varchar(100) null,
    dob date not null
);
insert thing(position,email,dob) values
(3,null,'1987-09-03'),(1,null,'1982-03-26'),
(2,'SamIAm@gmail.com','1976-10-03'),(2,'SamIAm@gmail.com','1976-10-03');
select * from thing;
+----------+------------------+------------+
| position | email            | dob        |
+----------+------------------+------------+
|        3 | NULL             | 1987-09-03 |
|        1 | NULL             | 1982-03-26 |
|        2 | SamIAm@gmail.com | 1976-10-03 |
|        2 | SamIAm@gmail.com | 1976-10-03 |
+----------+------------------+------------+

alter table thing add id int auto_increment primary key;

使用连接模式删除,删除后续的欺骗(具有更大的 id 号)

delete thing
from thing
join
( select position,email,dob,min(id) as theMin,count(*) as theCount 
  from thing 
  group by position,email,dob 
  having theCount>1
) xxx -- alias
on thing.position=xxx.position and thing.email=xxx.email and thing.dob=xxx.dob and thing.id>xxx.theMin
-- 1 row affected

从事物中选择 *;

+----------+------------------+------------+----+
| position | email            | dob        | id |
+----------+------------------+------------+----+
|        3 | NULL             | 1987-09-03 |  1 |
|        1 | NULL             | 1982-03-26 |  2 |
|        2 | SamIAm@gmail.com | 1976-10-03 |  3 |
+----------+------------------+------------+----+

添加唯一索引

CREATE UNIQUE INDEX `thing_my_composite` ON thing (position,email,dob); -- forbid dupes hereafter

查看当前表架构

show create table thing;

CREATE TABLE `thing` (
  `position` int(11) NOT NULL,
  `email` varchar(100) DEFAULT NULL,
  `dob` date NOT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`),
  UNIQUE KEY `thing_my_composite` (`position`,`email`,`dob`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 2016-02-18
    • 2012-01-13
    • 1970-01-01
    • 2017-11-30
    • 2018-03-13
    • 2020-07-04
    相关资源
    最近更新 更多