我有一个表:删除冗余记录
+------------+------------------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------------------------------------------+------+-----+---------+-------+
| person_id1 | int(10) | NO | MUL | 0 | |
| person_id2 | int(10) | NO | MUL | 0 | |
| priority | smallint(5) | NO | | 0 | |
| link_type | enum('member_of_band','legal_name','performs_as','') | NO | | | |
+------------+------------------------------------------------------+------+-----+---------+-------+
有此表上没有主键,但也有person_id1指标,并在person_id2。
的问题是 - 我们有不一致的数据,例如,这个查询:
SELECT
COUNT(*) as c, person_id1, person_id2
FROM person_person
WHERE link_type = "member_of_band"
GROUP BY person_id1, person_id2
HAVING c > 1
LIMIT 10;
返回:
+---+------------+------------+
| c | person_id1 | person_id2 |
+---+------------+------------+
| 2 | 50674235 | 51048792 |
| 3 | 50674245 | 50715733 |
| 2 | 50674283 | 50712621 |
| 2 | 50674322 | 50714244 |
| 2 | 50674378 | 51048804 |
| 2 | 50674438 | 51048812 |
| 4 | 50674442 | 50715733 |
| 2 | 50674449 | 50716913 |
| 2 | 50674455 | 51048803 |
| 3 | 50674469 | 50715733 |
+---+------------+------------+
有没有办法去除所有多余的记录,并留下那些确定?
所有我想出是:
DELETE person_person FROM person_person
WHERE (person_id1, person_id2) IN (
SELECT
person_id1, person_id2
FROM person_person
WHERE link_type = "member_of_band"
GROUP BY person_id1, person_id2
HAVING COUNT(*) > 1
LIMIT 100
) AND link_type = "member_of_band";
但是,这将与双打删除所有记录,我需要删除只是增加一倍。
mysql> select * from person_person where person_id1 = 50674245 and person_id2 = 50715733;
+------------+------------+----------+----------------+
| person_id1 | person_id2 | priority | link_type |
+------------+------------+----------+----------------+
| 50674245 | 50715733 | 0 | member_of_band |
| 50674245 | 50715733 | 0 | member_of_band |
| 50674245 | 50715733 | 0 | member_of_band |
+------------+------------+----------+----------------+
你想其中的“双打”的删除和这将你保持(假设他们有不同的'priority'值)? – eggyal
其中任何一个。假设我们有: – nikita2206
我不明白的冗余是什么。顺便说一句,你可能也想看看[正火](http://en.wikipedia.org/wiki/Database_normalization)数据库。 – JJJ