我有4列不同的数字,每行是不同的组合,我的目标是删除删除表中的重复项。 由于这些是交换数字顺序的组合,我想删除交换的行并且只保留1(我猜测每个4个数字集将在表格中有24个不同的选项,所以我会这样做需要删除23并且只保留1)
表名:" 4numbers"
-------------------------
|id | e1 | e2 | e3 | e4 |
-------------------------
| 1 | 1 | 5 | 3 | 7 |
| 2 | 1 | 5 | 7 | 9 |
| 3 | 5 | 1 | 7 | 3 |
-------------------------
所以我需要删除一个有重复组合的行。例如,第1行或第3行之一
这是我尝试过的,但它进入了一个无限循环,我确信我的查询有问题。
<?php
$sql = mysql_connect("localhost", "root", "mysql");
if (!$sql) {
die("Could not connect: " . mysql_error());
}
mysql_select_db("combs");
$result = mysql_query("delete b from 4_even a join 4_even b on (a.e1 = b.e2 and a.e2 = b.e2 and a.e3 = b.e4 and a.e4 = b.e4) or (a.e2 = b.e1 and a.e1 = b.e2 and a.e4 = b.e3 and a.e3 = b.e4) where a.id < b.id;");
echo "success";
if (!$result) {
die("Could not delete. " . mysql_error());
}
?>
我的查询是我之前使用的另一个查询的改编,用于从2列而不是4列的表中删除重复项,它与它完美配合。所以我只需要调整它以支持4列。
mysql_query("delete b from 4_numbers a join 4_numbers b on (a.e1 = b.e2 and a.e2 = b.e2) or (a.e2 = b.e1 and a.e1 = b.e2) where a.id < b.id;")
答案 0 :(得分:0)
你可以使用这样的查询。
1)创建一个临时表 2)在temp中插入所有唯一的行 3)重命名表(原子),因此应用程序不会失败
CREATE TABLE tmp_4_even LIKE 4_even;
INSERT INTO tmp_4_even
SELECT e2.* FROM (
SELECT id FROM (
SELECT min(id) AS id, GROUP_CONCAT(e1 SEPARATOR '-') AS g
FROM ( SELECT id,e1 FROM 4_even UNION ALL SELECT id,e2 FROM 4_even UNION ALL SELECT id,e3 FROM 4_even UNION ALL SELECT id,e4 FROM 4_even
ORDER BY id,e1
) AS res
GROUP BY id
) res2
GROUP BY g
) e1
LEFT JOIN 4_even e2 ON e1.id = e2.id;
RENAME TABLE 4_even TO 4_even_org, tmp_4_even TO 4_even;
示例内部查询
mysql> SELECT * FROM (
-> SELECT min(id) AS id, GROUP_CONCAT(e1 SEPARATOR '-') AS g
-> FROM ( SELECT id,e1 FROM 4_even UNION ALL SELECT id,e2 FROM 4_even UNION ALL SELECT id,e3 FROM 4_even UNION ALL SELECT id,e4 FROM 4_even
-> ORDER BY id,e1
-> ) AS res
-> GROUP BY id
-> ) res2
-> GROUP BY g ;
+------+---------+
| id | g |
+------+---------+
| 1 | 1-3-5-7 |
| 4 | 5-7-1-5 |
+------+---------+
2 rows in set, 1 warning (0,00 sec)
mysql>
<强>样品强>
mysql> select * from 4_even;
+----+------+------+------+------+
| id | e1 | e2 | e3 | e4 |
+----+------+------+------+------+
| 1 | 1 | 5 | 3 | 7 |
| 2 | 1 | 5 | 7 | 3 |
| 3 | 5 | 1 | 7 | 3 |
| 4 | 5 | 1 | 7 | 5 |
+----+------+------+------+------+
4 rows in set (0,00 sec)
mysql> CREATE TABLE tmp_4_even LIKE 4_even;
Query OK, 0 rows affected (0,02 sec)
mysql>
mysql> INSERT INTO tmp_4_even
-> SELECT e2.* FROM (
-> SELECT id FROM (
-> SELECT min(id) AS id, GROUP_CONCAT(e1 SEPARATOR '-') AS g
-> FROM ( SELECT id,e1 FROM 4_even UNION ALL SELECT id,e2 FROM 4_even UNION ALL SELECT id,e3 FROM 4_even UNION ALL SELECT id,e4 FROM 4_even
-> ORDER BY id,e1
-> ) AS res
-> GROUP BY id
-> ) res2
-> GROUP BY g
-> ) e1
-> LEFT JOIN 4_even e2 ON e1.id = e2.id;
Query OK, 2 rows affected, 1 warning (0,00 sec)
Records: 2 Duplicates: 0 Warnings: 1
mysql>
mysql> RENAME TABLE 4_even TO 4_even_org, tmp_4_even TO 4_even;
Query OK, 0 rows affected (0,00 sec)
mysql> select * from 4_even;
+----+------+------+------+------+
| id | e1 | e2 | e3 | e4 |
+----+------+------+------+------+
| 1 | 1 | 5 | 3 | 7 |
| 4 | 5 | 1 | 7 | 5 |
+----+------+------+------+------+
2 rows in set (0,00 sec)
mysql>