下面说的是我的表
entryID ID Number locationId timestamp
331 136 7 1 5/30/2018 9:23
332 136 7 1 5/30/2018 9:25
333 136 9 1 5/30/2018 9:25
334 136 11 1 5/30/2018 9:25
335 136 11 1 5/30/2018 9:25
336 136 9 1 5/30/2018 9:26
337 137 1 2 6/4/2018 8:45
338 137 2 2 6/4/2018 8:47
339 137 7 2 6/4/2018 8:47
340 137 7 2 6/4/2018 8:47
我想通过ID和locationId过滤它们,删除重复的行(“Number”是带有重复值的列),并在查询由locationId = 1过滤的表后查看结果如下所示。
entryID ID Number locationId timestamp
332 136 7 1 5/30/2018 9:25
335 136 11 1 5/30/2018 9:25
336 136 9 1 5/30/2018 9:26
337 137 1 2 6/4/2018 8:45
338 137 2 2 6/4/2018 8:47
339 137 7 2 6/4/2018 8:47
340 137 7 2 6/4/2018 8:47
如果locationId = 2,则下面是我希望看到的结果。
entryID ID Number locationId timestamp
331 136 7 1 5/30/2018 9:23
332 136 7 1 5/30/2018 9:25
333 136 9 1 5/30/2018 9:25
334 136 11 1 5/30/2018 9:25
335 136 11 1 5/30/2018 9:25
336 136 9 1 5/30/2018 9:26
337 137 1 2 6/4/2018 8:45
338 137 2 2 6/4/2018 8:47
340 137 7 2 6/4/2018 8:47
答案 0 :(得分:0)
要求解释不清楚。然而,假设时间戳确实是一个时间戳,那么我怀疑你是在这样的事情之后...
(请记住在删除任何内容之前备份数据!!!)
DELETE x
FROM my_table x
LEFT
JOIN
( SELECT locationid
, number
, MAX(entryid) entryid
FROM my_table
GROUP
BY locationid
, number
) y
ON y.locationid = x.locationid
AND y.number = x.number
AND y.entryid = x.entryid
WHERE y.entryid IS NULL;