PSQL获得重复行

编程入门 行业动态 更新时间:2024-10-28 14:26:07
本文介绍了PSQL获得重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这样的表 -

id object_id product_id 1 1 1 2 1 1 4 2 2 6 3 2 7 3 2 8 1 2 9 1 1

我想删除除这些以外的所有行 -

I want to delete all rows except these-

1 1 1 4 2 2 6 3 2 9 1 2

基本上有重复的,我想删除它们,但是保持一个副本不变。

Basically there are duplicates and I want to remove them but keep one copy intact.

这是最有效的方式?

推荐答案

如果这是一次性的话,那么你可以简单地确定要保留的记录如下:

If this is a one-off then you can simply identify the records you want to keep like so:

SELECT MIN(id) AS id FROM yourtable GROUP BY object_id, product_id;

你想检查这个工作,然后再做下一件事,实际上会抛出记录。要实际删除您所做的重复记录:

You want to check that this works before you do the next thing and actually throw records out. To actually delete those duplicate records you do:

DELETE FROM yourtable WHERE id NOT IN ( SELECT MIN(id) AS id FROM yourtable GROUP BY object_id, product_id );

MIN(id)显然总是返回一组( object_id , product_id )的最低ID的记录。根据需要进行更改。

The MIN(id) obviously always returns the record with the lowest id for a set of (object_id, product_id). Change as desired.

更多推荐

PSQL获得重复行

本文发布于:2023-10-20 21:19:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1512157.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!