Search code examples
mysqlselectduplicatesfreeradius

How I can delete the result of this selection query in a single mysql command?


Table name radacct radacctid is the pk accountuniqueid is the almost unique string generated by subscribers at connection time.

This is the mysql query that ouputs duplicated entries I want delete the result in the same command y order to automate this process.

select *
from radacct
group by acctuniqueid
HAVING
(radacct.acctuniqueid > 1)

Solution

  • I assume you meant HAVING COUNT(acctuniqueid) > 1 in your HAVING clause. If you truly want to delete ALL duplicates (i.e. keeping NONE of the dupes):

    DELETE FROM
        radacct
    WHERE
        `acctuniqueid` IN (
            SELECT `acctuniqueid` FROM (
                SELECT `acctuniqueid`
                FROM radacct
                GROUP BY `acctuniqueid`
                HAVING COUNT(`acctuniqueid`) > 1
            ) x
        )
    ;
    

    The intermediary SELECT is necessary for MySQL to ignore the same table reference within a direct sub-select.

    Please be advised to perform a full table backup before the deletion.