Jump to content

[SOLVED] Finding and removing duplicate entries with more then 1 column


Azu

Recommended Posts

Let's say I have a database with 10 columns

 

a b c d e f g h i and j

 

I want to select or delete all rows where a c and e are duplicated

 

So

 

a c e

row1: 1 2 3

row2: 1 2 3

 

That would be a duplicate

 

row1: 1 2 3

row2: 1 2 2

Would not be a duplicate

 

And there are a bunch of other columns in it that don't matter.

 

 

Please tell me how I can do this ^_^

Link to comment
Share on other sites

Hi.. sorry for noisiness.. but I think I'm doing it wrong.. it seems to be returning all of the rows in the whole table.. most of which are not duplicates..

 

select concat(position_x,position_y,position_z) from creature group by concat(position_x,position_y,position_z);

 

That is the exact query I am using.

 

Thanks for your help so far btw ^^

Link to comment
Share on other sites

Hi, thanks dude! I just have 1 more little problem ^^

Error Code : 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' position_y, position_z) AS duplicate

FROM creature

GROUP BY concat(position_x,' at line 2

(0 ms taken)

 

Can you please tell me why it is doing this and how to fix :D? I run the query in sqlyog by the way.

Link to comment
Share on other sites

Oops one more question please :P replacing select with delete * doesn't seem to work.. I thought that was all I would have to change to make it get rid of the duplicates lol.. please tell me how I have to change it to remove duplicates.. thanks.. =]

Link to comment
Share on other sites

Hehe that's what I meant sorry. I replaced it with delete but it doesn't work it just gives an error :<

 

And I think that even if the delete went through it would mess up the database because it would remove it all not just the duplicates

 

 

E.G. if there is a row and there are two duplicates of it it will delete the 2 duplicates AND the row, thus removing all of that data, instead of just the duplicate?

 

Is there a way to fix this please? And also the error? rror Code : 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY concat bla bla bla....

It appears when I use delete instead of the select stuff. Not sure why it's happening. :S

Link to comment
Share on other sites

If you have a column that's guaranteed to be unique, either for the whole table or just among the subset otherwise duplicate rows, then you can do this using temporary tables.  Also, with this method, it can't matter which dupes you delete.

 

1) Create a temporary table

2) Insert unique rows into the table: INSERT INTO temp_table SELECT var1,var2,uniquevar FROM yourtable GROUP BY var1,var2 HAVING COUNT(uniquevar) > 1;

3) Delete offending rows: DELETE yourtable FROM yourtable,temp_table WHERE yourtable.var1=temptable.var1 AND yourtable.var2=temptable.var2 AND yourtable.uniquevar != temptable.uniquevar;

 

(You can't use a subquery because: "Currently, you cannot delete from a table and select from the same table in a subquery." [MySQL manual])

Link to comment
Share on other sites

Here is an option with two seperate queries (MAKE A BACKUP FIRST)

 

The first creates a string that contains a set of all the unique ids and the second removes all the record id's that do not match have a match in the set - this way each unique copy is still kept. (IMPORTANT: all the records will be removed if @ids is empty when when the DELETE query is executed)

 

SELECT group_concat(id SEPARATOR ',') AS ids FROM (
SELECT id FROM creature group by concat_ws(',', position_x, position_y, position_z)
) AS derived INTO @ids

DELETE FROM creature WHERE NOT find_in_set(id, @ids);

Link to comment
Share on other sites

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.