timmah1 Posted August 14, 2009 Share Posted August 14, 2009 I have a form that pulls feeds from a site, and inserts them into the database <?php $xml=("http://www.novafantasysports.com/xml/bbfpbasketballplayernews/node_feed/?User=bbfreepicks_nbaplayernews&pw=bbfpFFBP"); $xmlDoc = new DOMDocument(); $xmlDoc->load($xml); //get and output "<item>" elements $x=$xmlDoc->getElementsByTagName('article'); $sql = "INSERT INTO feeds_history(title, author, content, date_posted) VALUES"; for ($i=0; $i<=19; $i++) { $item_title=$x->item($i)->getElementsByTagName('title') ->item(0)->childNodes->item(0)->nodeValue; $item_author=$x->item($i)->getElementsByTagName('author') ->item(0)->childNodes->item(0)->nodeValue; $item_created=$x->item($i)->getElementsByTagName('created') ->item(0)->childNodes->item(0)->nodeValue; $item_desc=$x->item($i)->getElementsByTagName('content') ->item(0)->childNodes->item(0)->nodeValue; $sql .= "('" . mysql_real_escape_string($item_title). "', '". mysql_real_escape_string($item_author) . "', '". strip_tags(mysql_real_escape_string($item_desc)) . "', '". mysql_real_escape_string($item_created) . "')"; //echo ("<a href='" . $item_link . "'>" . $item_title . "</a>"); //echo ($item_desc); if($i < 19) { $sql .= ", "; } else { $sql .=";"; } } $result = mysql_query($sql); ?> Now I need to check it there are any duplicates, and then delete only the duplicates, except one from the database. So, if there are 3 of the title "Try This", I need to delete 2 of those rows. How would I go about doing this? Quote Link to comment https://forums.phpfreaks.com/topic/170268-duplicate-rows/ Share on other sites More sharing options...
DEVILofDARKNESS Posted August 14, 2009 Share Posted August 14, 2009 I read on a website: select distinct * into NewTable from MyTable delete T1 from MyTable T1, MyTable T2 where T1.dupField = T2.dupField and T1.uniqueField > T2.uniqueField the url hope this helps Kruptein Alias DEVILofDARKNESS Quote Link to comment https://forums.phpfreaks.com/topic/170268-duplicate-rows/#findComment-898350 Share on other sites More sharing options...
roopurt18 Posted August 14, 2009 Share Posted August 14, 2009 First off you could add a unique key to the table so they don't get in there in the first place. As far as deleting them, you can do it without the temp table if your existing table has a column that is different for every row, such as a primary key. Does your table have such a column? Quote Link to comment https://forums.phpfreaks.com/topic/170268-duplicate-rows/#findComment-898371 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.