1
votes

I created a query to delete some record in MariaDB

Query :

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 
)

Result :

Table 'HrAttLogsFormatted' is specified twice, both as a target for 'DELETE' and as a separate surce for data

But with the query I made unsuccessfully, is there a way to solve it?

Thank you in advance

[EDIT-SOLVED] It's Solved with simply apply and query

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;
1
I can give you a workaround right now, but I would ask that you please clearly tell us the logic happening here. My guess is that we can rewrite your query in a simpler way.Tim Biegeleisen
Ok, thank you in advance. In this case I have the same 2 data where the first data is created at the beginning and the second data is created at the end ... from both of these data I want to delete the second data where this data was made last @TimBiegeleisenAldan

1 Answers

1
votes

I would try writing this as a delete join:

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;

If you really wanted to stick with your current query, you might be able to make it work by adding an additional subquery around the WHERE IN clause:

AND (FingerId, CreatedDate) IN (
    SELECT FingerId, MinCreatedDate
    FROM
    (
        SELECT FingerId, MIN(CreatedDate) AS MinCreatedDate
        FROM HrAttLogsFormatted
        ...
    ) x )

But, I would use the version I gave, because it is simpler.