如何删除'复制'记录?

时间:2015-08-21 11:35:28

标签: mysql sql

我有一张包含一些损坏记录的表,因为我忘记了 为两列添加+----+-------------+--------+------------+ | id | uuid | object | project_id | +----+-------------+--------+------------+ | 1 | 73621000001 | screw | 1 | | 2 | 73621000002 | screw | 1 | | 3 | 73621000003 | screw | 1 | | 4 | 73621000004 | tube | 1 | | 5 | 73621000005 | plate | 2 | | 6 | 73621000006 | plate | 2 | | 7 | 73621000007 | plate | 2 | | 8 | 73621000008 | plate | 2 | | 9 | 73621000009 | plate | 2 | | 10 | 73621000010 | gear | 4 | | 11 | 73621000011 | gear | 4 | +----+-------------+--------+------------+ 索引。 请查看下表中的示例:

object

正如您所看到的,有一些project_id - uuids - 组合多次出现,但却有不同的uuid。 我想删除所有重复记录,但保留最高+----+-------------+--------+------------+ | id | uuid | object | project_id | +----+-------------+--------+------------+ | 3 | 73621000003 | screw | 1 | | 4 | 73621000004 | tube | 1 | | 9 | 73621000009 | plate | 2 | | 11 | 73621000011 | gear | 4 | +----+-------------+--------+------------+ 的记录。 结果表应为:

object

我可以使用以下查询查看哪些SELECT uuid, object, project_id, COUNT(*) FROM uuid_object_mapping GROUP BY object, project_id HAVING COUNT(*) > 1; 有重复项:

SELECT MAX(uuid) as uuid, object, project_id
FROM uuid_object_mapping
GROUP BY object, project_id;

我可以清理'使用此查询的表:

SELECT uuid, object, project_id, COUNT(*)
FROM (
    SELECT MAX(uuid) as uuid, object_name, project_id
    FROM uuid_object_mapping
    GROUP BY object_name, project_id
) AS clean
GROUP BY object_name, project_id
HAVING COUNT(*) > 1;

我可以验证清洁'表格不包含使用

的重复项
x <- c(35, 2, 3, 30, 1, 4, 33, 6, 36)

但是如何删除“清洁”中没有的所有内容?表

3 个答案:

答案 0 :(得分:2)

在MySQL中,您可以使用if (count($results) > 0) { foreach ($results as $row) { echo '<li><kef>' . $row['title'] . '</kef></br>'; if ($row['mediaType'] === 'image') { echo '<img src=' . $row['url'] . '></br>'; /*here */ } if ($row['mediaType'] === 'video') { echo '<kef1>' . $row['url'] . '</kef1></br>'; /*and here*/ } echo '<kef2>' . $row['desc'] . '</kef2></br>'; echo '<kef3>' . $row['tag'] . '</kef3></br> </br> </li>'; } } ,但需要注意//any not sorted map Map<String, Integer> map = new HashMap<>(); map.put("567", 567); map.put("456", 456); map.put("123", 123); //create sorted TreeMap with descending sorting Map<String, Integer> sortedMap = new TreeMap<String, Integer>(Collections.reverseOrder()); sorted.putAll(map); 值:

private static Map<String, Integer> sortByComparator(Map<String, Integer> unsortedMap) {

    List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(unsortedMap.entrySet());

    Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
        public int compare(Map.Entry<String, Integer> o1,
                           Map.Entry<String, Integer> o2) {
            return (o2.getValue()).compareTo(o1.getValue());
        }
    });

    Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
    for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext();) {
        Map.Entry<String, Integer> entry = it.next();
        sortedMap.put(entry.getKey(), entry.getValue());
    }
    return sortedMap;
}

join值似乎已消失,因此您可以使用此NULL子句:

delete om
    from uuid_object_mapping om join
         (select MAX(uuid) as uuid, object, project_id
          from uuid_object_mapping 
          group by object, project_id
         ) omkeep
         on omkeep.object = om.object and
            omkeep.project_id <=> om.project_id
    where om.uuid <> omkeep.uuid;

答案 1 :(得分:0)

请按照uuid条款的顺序使用分区。搜索分享。这是删除重复的最佳技术。

答案 2 :(得分:-1)

试试这个

select t1.id,t1.uuid,t1.object,t1.project_id from table as t1 inner join
(
select object,max(id) as id from table
group by object
) as t2 on t1.object=t2.object and t1.id=t2.id