user4fun Posted September 26, 2009 Share Posted September 26, 2009 I am trying to write a select statment and then run through the results and delete all entries except the one with the highest Index number (AUTO INCREMENT)what is the easiest way to do this. I do want to have toselect ORDER by Index_NumCount number of rowsgo through a loop statment deleting the records until the row number is equal to the row_Count number.yada yada yadaIs there any easier way. I am to Just find the last record saved WHERE tbl.Column = '$value'; and then delete all others that have the same "WHERE" clause. Link to comment Share on other sites More sharing options...
Synook Posted September 26, 2009 Share Posted September 26, 2009 What about just DELETE FROM table ORDER BY Index_Num DESC LIMIT 1 Link to comment Share on other sites More sharing options...
user4fun Posted September 27, 2009 Author Share Posted September 27, 2009 That looks very simple. It will order in a decending matter. But how would I use the first row that comes and delete the rest? that if if there is even any rows.Thank you Link to comment Share on other sites More sharing options...
Synook Posted September 28, 2009 Share Posted September 28, 2009 Whoops, sorry, that is to delete the last record. Hmm... Link to comment Share on other sites More sharing options...
justsomeguy Posted September 28, 2009 Share Posted September 28, 2009 Run one query to get the max ID, and another to delete everything with a smaller ID. Link to comment Share on other sites More sharing options...
ulisses Posted September 28, 2009 Share Posted September 28, 2009 Hi! My idea seems like the one justsomeguy told.U run a query to select the max id, and another one to delete all the others less than the max id.I mean something like:1st query $max=mysql_query("SELECT MAX(id_column_name) AS max_id FROM table_name WHERE column_name=$value"); $maxi=mysql_fetch_array($max); $big=$maxi['max_id']; 2nd query mysql_query("DELETE FROM table_name WHERE column_name=$value AND id_column_name < $big "); I hope this can help u! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.