blueman378 Posted February 1, 2008 Share Posted February 1, 2008 hi guys well im trying to write a code which orders all results in a table by the "time" column and if there is more than 20 it will delete the oldest one i have this so far $q = "SELECT * FROM latest ORDER BY `time` ASC "; $result = $database->query($q) or die("Error: " . mysql_error()); /* Error occurred, return given name by default */ $num_rows = mysql_numrows($result); if( $num_rows == 20 ){ mysql_query("DELETE FROM latest"); } but im kinda stuck as how to tell it to delete the result with the oldest time cheers matt Quote Link to comment https://forums.phpfreaks.com/topic/88871-mysql-delete/ Share on other sites More sharing options...
beansandsausages Posted February 1, 2008 Share Posted February 1, 2008 try : $q = "SELECT * FROM latest ORDER BY `time` ASC"; $result = $database->query($q) or die("Error: " . mysql_error()); /* Error occurred, return given name by default */ $num_rows = mysql_num_rows($result); if( $num_rows == 20 ){ mysql_query("DELETE FROM latest"); } Quote Link to comment https://forums.phpfreaks.com/topic/88871-mysql-delete/#findComment-455200 Share on other sites More sharing options...
blueman378 Posted February 1, 2008 Author Share Posted February 1, 2008 well thats not what i mean i mean i need to extend $q = "SELECT * FROM latest ORDER BY `time` ASC"; $result = $database->query($q) or die("Error: " . mysql_error()); /* Error occurred, return given name by default */ $num_rows = mysql_num_rows($result); if( $num_rows == 20 ){ mysql_query("DELETE FROM latest WHERE !!!HERE!!!"); } to make it delete the oldest results cheers Quote Link to comment https://forums.phpfreaks.com/topic/88871-mysql-delete/#findComment-455202 Share on other sites More sharing options...
psychowolvesbane Posted February 1, 2008 Share Posted February 1, 2008 Have you tried ordering the records into descending order in your sql selection? Quote Link to comment https://forums.phpfreaks.com/topic/88871-mysql-delete/#findComment-455207 Share on other sites More sharing options...
beansandsausages Posted February 1, 2008 Share Posted February 1, 2008 DELTE * FROM `table` WHERE id >= 20 thats if your only allowing 20 records will delete all ov them above 20 Quote Link to comment https://forums.phpfreaks.com/topic/88871-mysql-delete/#findComment-455210 Share on other sites More sharing options...
blueman378 Posted February 3, 2008 Author Share Posted February 3, 2008 well that could work buit the data is inserted into the table with 2 columns one is simply name the other is time which is something like 020220080156 sort thing which is 2nd of the 2nd 2008 at 1:56 am so i order using that not ids, Quote Link to comment https://forums.phpfreaks.com/topic/88871-mysql-delete/#findComment-456522 Share on other sites More sharing options...
AndyB Posted February 3, 2008 Share Posted February 3, 2008 well that could work buit the data is inserted into the table with 2 columns one is simply name the other is time which is something like 020220080156 sort thing which is 2nd of the 2nd 2008 at 1:56 am ... Inventing your own method of storing datetime is your mistake. Use a real datetime stamp and your problems will evaporate. Quote Link to comment https://forums.phpfreaks.com/topic/88871-mysql-delete/#findComment-456523 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.