简体   繁体   中英

SQL delete query from Table

Hello everyone I want to remove any redundancy of lines in the circulaire table with the same code product(code_prd) by year (annee), object(objet), sector(secteur), circular number ( num_circulaire ) except product code equal to "-"

sql code of the table

CREATE TABLE `circulaire` (
  `id` int(11) NOT NULL,
  `code_prd` varchar(255) DEFAULT NULL,
  `num_circulaire` double DEFAULT NULL,
  `annee` double DEFAULT NULL,
  `date` datetime DEFAULT NULL,
  `objet` double DEFAULT NULL,
  `libelle_prd` varchar(100) DEFAULT NULL,
  `secteur` varchar(1) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

This looks like MySQL. You can use delete with join :

delete c
    from circulaire c left join
         (select code_prd, annee, objet, secteur, num_circulaire, min(id) as min_id
          from circulaire
          group by code_prd, annee, objet, secteur, num_circulaire
         ) cc
         on c.id = cc.min_id
     where c.code_prd <> '-' and
           cc.min_id is null;

This calculates the minimum id for the combination of columns you have specified. The LEFT JOIN finds matches on the minimum id for those columns. Only non-matches are deleted.

Try this:

DELETE
FROM
    circulaire 
WHERE
    id IN(
    SELECT
        *
    FROM
        (
        SELECT
            MIN(id)
        FROM
            circulaire 
        GROUP BY
            code_prd, annee, objet, secteur, num_circulaire
        HAVING
            COUNT(*) > 1
    ) temp
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM