Pythondesigns Posted July 14, 2006 Share Posted July 14, 2006 I have a table in a MySql database like this:idurlI know that there are some rows which have got the same url. How can I check for these in PHP and display them.... Link to comment https://forums.phpfreaks.com/topic/14583-check-for-duplicates-in-a-mysql-database/ Share on other sites More sharing options...
hvle Posted July 14, 2006 Share Posted July 14, 2006 you can group them by url:"select count(*) as count, url from table group by url";returned results contains 2 column: count and url.count contain the number of repetition of url.you can use this result to find out the id contain repeated url:"select id where url='repeated_url'"; Link to comment https://forums.phpfreaks.com/topic/14583-check-for-duplicates-in-a-mysql-database/#findComment-57894 Share on other sites More sharing options...
brown2005 Posted July 14, 2006 Share Posted July 14, 2006 do a select SELECT *, COUNT(*) AS NUM FROM TABLE GROUP BY NUM DESC, URL ASCTHIS WILL COUNT HOW MANY OF EACH URL THERE ARE AND SHOW U LIKE THISURL1 10URL4 10URL3 4URL99 1ETC, ETC...HOPE THIS HELPS Link to comment https://forums.phpfreaks.com/topic/14583-check-for-duplicates-in-a-mysql-database/#findComment-57897 Share on other sites More sharing options...
wildteen88 Posted July 14, 2006 Share Posted July 14, 2006 Also to stop duplicate URLs from being entered into the database you should set the url field to be UNIQUE which will prevent duplicate entries from being entered into the url field. Link to comment https://forums.phpfreaks.com/topic/14583-check-for-duplicates-in-a-mysql-database/#findComment-57921 Share on other sites More sharing options...
Barand Posted July 14, 2006 Share Posted July 14, 2006 [quote author=hvle link=topic=100562.msg396988#msg396988 date=1152884904]you can group them by url:"select count(*) as count, url from table group by url";returned results contains 2 column: count and url.count contain the number of repetition of url.you can use this result to find out the id contain repeated url:"select id where url='repeated_url'";[/quote]To get just the duplicates"select count(*) as count, url from table group by url having count > 1"; Link to comment https://forums.phpfreaks.com/topic/14583-check-for-duplicates-in-a-mysql-database/#findComment-57928 Share on other sites More sharing options...
hvle Posted July 15, 2006 Share Posted July 15, 2006 even better, url with most count first:"select count(*) as count, url from table group by url having count > 1 order by count desc";having count > 1; I've learn new thing to day Link to comment https://forums.phpfreaks.com/topic/14583-check-for-duplicates-in-a-mysql-database/#findComment-58199 Share on other sites More sharing options...
Pythondesigns Posted July 15, 2006 Author Share Posted July 15, 2006 Thanks for all the replies :DPerfect :D Link to comment https://forums.phpfreaks.com/topic/14583-check-for-duplicates-in-a-mysql-database/#findComment-58559 Share on other sites More sharing options...
pixy Posted July 15, 2006 Share Posted July 15, 2006 You could just place a UNIQUE index on the column, so that way it would be impossible to insert a duplicate record. Link to comment https://forums.phpfreaks.com/topic/14583-check-for-duplicates-in-a-mysql-database/#findComment-58560 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.