简体   繁体   English

MySQL删除同一表中的重复项

[英]MySQL delete duplicates in same table

I've been slamming my head on this problem and viewed a few similar situations on stack exchange and the more I read the more confused I get! 我一直在猛烈抨击这个问题,并在堆栈交换中查看了一些类似的情况,而且阅读得越多,我就会感到困惑! I have a table that has the following: 我有一个包含以下内容的表:

id | zipcode | provider | channel
---------------------------------
1  | 91773   | 342      | 0
2  | 91773   | 2261     | 177
3  | 91773   | 5590     | 0
4  | 91773   | 5590     | 0
5  | 91773   | 5590     | 135
6  | 91773   | 5590     | 0
7  | 91773   | 6010     | 0
8  | 91773   | 6010     | 0

I want to keep only one record and if the channel has anything but a 0 then we keep it. 我只想保留一条记录,如果频道中只有0记录,那么我们保留它。 So it should return this: 因此它应该返回以下内容:

id | zipcode | provider | channel
---------------------------------
1  | 91773   | 342      | 0
2  | 91773   | 2261     | 177
5  | 91773   | 5590     | 135
8  | 91773   | 6010     | 0

I tried quite a few queries, but none worked. 我尝试了很多查询,但没有一个起作用。 Thanks in advance. 提前致谢。

Edit: I've tried some of the examples given, but none seem to give back the correct info, a better example would be to use these and you can see why: insert into unicorns values 编辑:我已经尝试过给出的一些示例,但是似乎都没有给出正确的信息,一个更好的示例是使用这些示例,您可以看到原因:插入到独角兽值中

(1, 91773, 342, 0),
(2, 91773, 2261, 177),
(3, 91773, 5590, 0),
(4, 91773, 5590, 0),
(5, 91773, 5590, 135),
(6, 91773, 5590, 0),
(7, 91773, 6010, 0),
(8, 91773, 6010, 0),
(9, 91776, 5590, 135),
(10, 91776, 5590, 0),
(11, 91776, 6010, 0),
(12, 91776, 6010, 0);

Something like this should work... 这样的事情应该起作用...

DELETE unicorns 
FROM   unicorns 
WHERE  id NOT IN (SELECT id 
                  FROM   (SELECT unicorns.provider, 
                                 Max(id) AS id 
                          FROM   unicorns 
                                 LEFT JOIN (SELECT provider, 
                                                   Max(channel) AS channel 
                                            FROM   unicorns 
                                            GROUP  BY provider) p 
                                        ON p.provider = unicorns.provider 
                                           AND p.channel = unicorns.channel 
                          WHERE  p.provider IS NOT NULL 
                          GROUP  BY unicorns.provider) p2) 

See the demo 观看演示

You can perform this using a self join. 您可以使用自连接执行此操作。 If you are just doing the delete: 如果您只是删除:

DELETE a
FROM foo a 
  JOIN foo b on (a.zipcode = b.zipcode AND a.provider = b.provider)
WHERE 
  a.channel < b.channel;

If you want to see the data before you delete(what will be left): 如果要在删除之前查看数据(剩下的内容):

SELECT * 
FROM foo
WHERE ID NOT IN (
 SELECT a.id
 FROM foo a 
   JOIN foo b on (a.zipcode = b.zipcode AND a.provider = b.provider)
 WHERE 
   a.channel < b.channel);

SQL Fiddle demo SQL Fiddle演示

I think you could use something like this: 我认为您可以使用以下方式:

delete from unicorns
where id not in (
  SELECT * FROM (
    SELECT MAX(id)
    FROM unicorns
    WHERE (provider, zipcode, channel) IN (
      SELECT provider, zipcode, max(channel) mx_channel
      FROM unicorns
      GROUP BY provider, zipcode
    )
    GROUP BY provider, zipcode) s)

Please see fiddle here . 请看这里的小提琴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM