Jump to content

need a query for removing dupes


webosb

Recommended Posts

i cant seem to figure out how i can do this but this is what i need done.

I have 3 fields: id, rating, and ip

 

sample format:

 

id | rating | ip

1 | 10 | 1.2.3.4

1 | 10 | 1.2.3.4

1 | 10 | 1.2.3.4

1 | 10 | 1.2.3.4

1 | 10 | 1.3.2.4

1 | 10 | 1.1.2.2

 

I need a query to remove all duplicate ips for rating 10 for id 1 so the above sample will end up like this:

 

id | rating | ip

1 | 10 | 1.2.3.4

1 | 10 | 1.3.2.4

1 | 10 | 1.1.2.2

 

please note that id ranges from 1-1000 and rating ranges anywhere from 1 to 10 so I need a query to remove dupe ip's from ratings 1-10 for each id

Link to comment
https://forums.phpfreaks.com/topic/54361-need-a-query-for-removing-dupes/
Share on other sites

That is oddly stored data, consider grabing a count of each group that has duplicate records, like this:

 

SELECT count(*) - 1 as delete_number, concat(id, rating, ip) as duplicate_row 
FROM ips
GROUP BY concat(id, rating, ip)
HAVING count(*) > 1;

 

... and then deleting each duplicate ip record separately with a limit

 

mysql_query($query = "DELETE FROM ips WHERE concat(id, rating, ip) = '{$record['duplicate_row']}' LIMIT {$record['delete_number']}") or trigger_error(mysql_error()."<PRE>".$query."</PRE>", E_USER_ERROR);

 

EDIT: probably best to add an appropriate separator for the concat()

?

I mean, according the query I have written there It removes all the records except the first occurrence of each unique ip .As each ip has a unique id and rank so it will keep the records of unique (id, rating, ip) combination.

webosb original post does not mention that each ip has a unique id - but does mention that the ids range from 1 - 1000 and rating range from 1 - 10.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.