Jump to content

Recommended Posts

Sometimes i have rows that are duplicates - but with different primary ids

 

Table name: photo

Column name: title

 

I need to select only the rows that are duplicates

 

I tried a distinct query but it just return all rows

SELECT DISTINCT title FROM photo 

Link to comment
https://forums.phpfreaks.com/topic/144896-selecting-duplicate-rows/
Share on other sites

So which fields are duplicated? This is key to the query.

I'll take a guess:

 

SELECT p1.id, p1.title, p2.id, p2.title
FROM photo p1
JOIN photo p2 ON p1.title = p2.title AND p2.id != p1.id

 

The above should give you all the id's (assuming "id" is the name of your primary key) and title's of duplicated records.

 

This also points to an issue with indexes. If your titles need to be unique make sure your application checks for duplicate titles before it starts the insert, AND you have a UNIQUE index on the title field, to stop duplicate entries.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.