我创建了一个查询以删除MariaDB中的某些记录
查询:
DELETE
FROM
HrAttLogsFormatted
WHERE
DateIn = '2019-04-10'
AND Late != ''
AND ( FingerId, CreatedDate ) IN (
SELECT
FingerId,
MAX( CreatedDate )
FROM
HrAttLogsFormatted
WHERE
DateIn = '2019-04-10'
AND Late != ''
AND FingerId IN ( SELECT FingerId FROM HrAttLogsFormatted WHERE DateIn = '2019-04-10' AND Late != '' GROUP BY FingerId HAVING COUNT( FingerId ) > 1 )
GROUP BY
FingerId
)
结果:
表'HrAttLogsFormatted'被指定了两次,都作为目标 “删除”并作为数据的单独来源
但是对于我未成功进行的查询,有没有解决的方法?
提前谢谢
[EDIT-SOLVED]只需应用和查询即可解决
DELETE t1
FROM HrAttLogsFormatted t1
INNER JOIN
(
SELECT FingerId, MinCreatedDate
FROM
(
SELECT FingerId, MIN(CreatedDate) AS MinCreatedDate
FROM HrAttLogsFormatted
WHERE DateIn = '2019-05-03' AND Late != ''
GROUP BY FingerId HAVING COUNT(FingerId) > 1
) x
) t2
ON t1.FingerId = t2.FingerId AND t1.CreatedDate = t2.MinCreatedDate;
答案 0 :(得分:1)
我会尝试将其写为删除联接:
DELETE t1
FROM HrAttLogsFormatted t1
INNER JOIN
(
SELECT FingerId, MIN(CreatedDate) AS MinCreatedDate
FROM HrAttLogsFormatted
WHERE DateIn = '2019-04-10' AND Late != ''
GROUP BY FingerId
HAVING COUNT(FingerId) > 1
) t2
ON t1.FingerId = t2.FingerId AND t1.CreatedDate = t2.MinCreatedDate;
如果您真的想继续使用当前查询,则可以通过在WHERE IN
子句周围添加一个附加子查询来使其正常工作:
AND (FingerId, CreatedDate) IN (
SELECT FingerId, MinCreatedDate
FROM
(
SELECT FingerId, MIN(CreatedDate) AS MinCreatedDate
FROM HrAttLogsFormatted
...
) x )
但是,我会使用我给的版本,因为它更简单。