0

I have a table with duplicate title and I can query them just fine when trying to find.

Here's the table and code

Table

TABLE NAME: documents

+---------- +--------+
| module_srl| title  |
+---------- +--------+
| 6225      | test21 |
| 6225      | test   |
| 6225      | test   |
| 6226      | test3  |
| 6226      | test5  |
| 6226      | test6  |
+-----------+--------+

SQL QUERY:

SELECT title
FROM `documents`
WHERE module_srl = 6225
GROUP BY title
HAVING COUNT( * ) >1
ORDER BY `documents`.`title` DESC

THIS SHOWS RESULT OF 1 which has 1 duplicate found (I guess)

Title
test

How do I delete "only" the duplicate title that matches module_srl = 6225 without deleting the original.

squancy
  • 565
  • 1
  • 7
  • 25
danny
  • 13
  • 2

1 Answers1

0

You need the column identifier to differentiate the duplicate column.

The below query can delete any duplicate, instead of limit to module_srl 6255. if you want to limit to 6255, you can edit to add the where clause.

SqlFiddle

delete from documents
where document_srl in (
    select document_srl
    from (
        select module_srl, min(document_srl) as document_srl
        from documents
        group by module_srl, title
        having count(*) > 1
    ) t
Jacob
  • 1,776
  • 14
  • 11