为什么不删除所有重复项?

时间:2018-12-03 17:41:56

标签: sql sqlite

SQLite

此处数据

   |  80 |    1536843922000 | 24440271 | 26B008D0-EDA3-4F83-9AFF-6C04BBB1BF33 |
   | 109 |    1536843922000 | 36656927 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |
   |  83 |    1536843922000 | 94020805 | 26B008D0-EDA3-4F83-9AFF-6C04BBB1BF33 |

   |  77 |    1536930322000 | 22240444 | 26B008D0-EDA3-4F83-9AFF-6C04BBB1BF33 |
   |  53 |    1536930322000 | 24424792 | 26B008D0-EDA3-4F83-9AFF-6C04BBB1BF33 |
   |  79 |    1536930322000 | 24440271 | 26B008D0-EDA3-4F83-9AFF-6C04BBB1BF33 |
   | 113 |    1536930322000 | 37473218 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |
   | 115 |    1536930322000 | 38483615 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |
   | 116 |    1536930322000 | 38483615 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |
   |  47 |    1537016722000 | 24253674 | 26B008D0-EDA3-4F83-9AFF-6C04BBB1BF33 |
   | 105 |    1537016722000 | 34664388 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |
   | 117 |    1537016722000 | 38483615 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |
   |  58 |    1537103122000 | 37045208 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |
   |  59 |    1537103122000 | 37045208 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |

这里是删除重复项的sqlite查询:

DELETE FROM notification_invoice WHERE notificationDate >= 1536883200000   AND providerId in ("26B008D0-EDA3-4F83-9AFF-6C04BBB1BF33","9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4") 
     AND EXISTS (
            SELECT 1
              FROM notification_invoice t2
             WHERE t2.providerId in ("26B008D0-EDA3-4F83-9AFF-6C04BBB1BF33","9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4") 
               AND t2.notificationDate = notification_invoice.notificationDate
               AND notification_invoice.notificationDate >= 1536019200000
               AND t2.ownerKey  = notification_invoice.ownerKey 
               AND t2._id > notification_invoice._id
       )

结果如下:

   | _id | notificationDate | ownerKey | providerId                           |
   |-----+------------------+----------+--------------------------------------|
   |  58 |    1537103122000 | 37045208 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |
   | 115 |    1536930322000 | 38483615 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |

否,我通过获取所有数据来检查结果:

select _id, notificationDate, ownerKey, providerid from notification_invoice where notificationDate >= 1536883200000 and providerId in ("26B008D0-EDA3-4F83-9AFF-6C04BBB1BF33","9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4") order by  ownerKey, notificationDate

此处结果:

| _id | notifDate | ownerKey | providerId                           |
|-----+------------------+----------+--------------------------------------|
|  77 |    1536930322000 | 22240444 | 26B008D0-EDA3-4F83-9AFF-6C04BBB1BF33 |
|  47 |    1537016722000 | 24253674 | 26B008D0-EDA3-4F83-9AFF-6C04BBB1BF33 |
|  53 |    1536930322000 | 24424792 | 26B008D0-EDA3-4F83-9AFF-6C04BBB1BF33 |
|  79 |    1536930322000 | 24440271 | 26B008D0-EDA3-4F83-9AFF-6C04BBB1BF33 |
| 105 |    1537016722000 | 34664388 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |
|  59 |    1537103122000 | 37045208 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |
| 113 |    1536930322000 | 37473218 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |
| 116 |    1536930322000 | 38483615 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |
| 117 |    1537016722000 | 38483615 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |

在成功案例中,请删除除一个以外的所有重复项:

| 116 |    1536930322000 | 38483615 | 9856BAB6-51BE-4EA8-B0D7-72ABF4A452A4 |

为什么不删除此记录?

1 个答案:

答案 0 :(得分:1)

对于示例数据中的_id = 116,只有1个_id = 117,例如
t2._id > notification_invoice._id
但对于这2个ID(116和117),此条件:

t2.notificationDate = notification_invoice.notificationDate

失败。
因此EXIST返回false