我想创建一个脚本,它将保留所有重复项并获得每个重复值的最低id,并用它填充所有重复项的ID_TO_KEEP字段。
我想要发生什么:
+-----+----------------+-----------+-----------+
| id | name |address |id_to_keep |
+-----+----------------+-----------+-----------+
| 131 | google company |california |131 |
| 223 | google inc. |california |131 |
| 334 | msn |boston | |
| 442 | facebook |california | |
| 543 | yahoo inc. |washington |543 |
| 621 | yahoo limited |washington |543 |
+-----+----------------+-----------+-----------+
在上面的示例中,“google company”和“google inc。”将被视为重复,因为它们只是同一家公司,然后脚本必须比较他们的id并获得最低的id然后填充id_to_keep字段,其中id最低。
希望有人可以帮助我。在Excel宏或SQL脚本
上答案 0 :(得分:0)
您可能正在寻找类似的东西...但请注意,您可能需要一种更强大的方法来确定具有相似名称的两家公司是否实际上是相同的......
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,name VARCHAR(30) NOT NULL
,address VARCHAR(20) NOT NULL
);
INSERT INTO my_table VALUES
(131 ,'google company','california'),
(223 ,'google inc.','california'),
(334 ,'msn','boston'),
(442 ,'facebook','california'),
(543 ,'yahoo inc.','washington'),
(621 ,'yahoo limited','washington');
SELECT x.*
FROM my_table x
JOIN
( SELECT MIN(id) min_id FROM my_table GROUP BY SUBSTRING_INDEX(name,' ',1),address ) y
ON y.min_id = x.id;
+-----+----------------+------------+
| id | name | address |
+-----+----------------+------------+
| 442 | facebook | california |
| 131 | google company | california |
| 334 | msn | boston |
| 543 | yahoo inc. | washington |
+-----+----------------+------------+
将其重写为DELETE ...
是微不足道的 DELETE x
FROM my_table x
LEFT
JOIN
( SELECT MIN(id) min_id FROM my_table GROUP BY SUBSTRING_INDEX(name,' ',1),address ) y
ON y.min_id = x.id
WHERE y.min_id IS NULL;
SELECT * FROM my_table;
+-----+----------------+------------+
| id | name | address |
+-----+----------------+------------+
| 131 | google company | california |
| 334 | msn | boston |
| 442 | facebook | california |
| 543 | yahoo inc. | washington |
+-----+----------------+------------+
答案 1 :(得分:0)
您也可以使用变量(我假设该表名为test_test):
SELECT
id,
@last_id := IF(@lastGroupID!=SUBSTRING_INDEX(test_test.name,' ',1),test_test.id,@last_id) as id_to_keep,
@lastGroupID := SUBSTRING_INDEX(test_test.name,' ',1) as company,
address
FROM bigwf.test_test, (select @lastGroupID:=null) as lastGroupID,(select @last_id:= (select min(id) from test_test)) as lastID
order by id
=>这会给你
+-----+------------+----------+------------+
| id | id_to_keep | company | address |
+-----+------------+----------+------------+
| 131 | 131 | google | california |
| 223 | 131 | google | california |
| 334 | 334 | msn | boston |
| 442 | 442 | facebook | california |
| 543 | 543 | yahoo | washington |
| 621 | 543 | yahoo | washington |
+-----+------------+----------+------------+
编辑:假表(选择@i:= 0)不需要rownum_table。