I have a table to store messages. I want to store only latest 50 messages how to delete the records previous to latest 50 records?
Just on on the output LIMIT to 50? I would need to know your database design to see exactly how to successfully delete them, Usually it's something around mysql_query("DELETE FROM Table_name WHERE row_name > 50");
http://dev.mysql.com/doc/refman/5.0/en/delete.html something like: DELETE FROM tablenamegoeshere LIMIT (50-$number_of_records);
It depends. Start by running the following query on the database, replacing "TABLE_NAME" with your message tables name, and post the results. If you're using something like phpMyAdmin, a screenshot of the table's structure from there will work too. DESCRIBE TABLE_NAME; Code (sql): The result from that SQL query will look something like this mysql> describe visitors; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | keyword | varchar(32) | NO | | NULL | | | userid | int(11) | NO | | NULL | | +---------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) Code (markup):
What you want to achieve will not work unless you have a date/timestamp field. Unless ofcourse the unique keys are generated by you and not automatically, then it depends on the algorithm you are using to create the keys.
Thanks to all.I done it... $max_id=mysql_fetch_array(mysql_query("SELECT count(id) as id FROM mytable")); $number_of_records=$max_id[id]; $limit=$number_of_records-50; if(!($limit<1)) { mysql_query("DELETE FROM mytable ORDER BY id ASC LIMIT $limit")or die(mysql_error()); }