This article mainly introduces the PHP implementation to keep only the latest 1000 records in MySQL method and related examples and database structure, very comprehensive, the need for small partners can refer to.
?
123456789101112131415161718192021 |
<?phpmysql_connect ("localhost", "root", "root"); mysql_select_db ("test");//Keep the latest 1000 records $limit=1000; $query = " Select ' id ' from ' News ', $result =mysql_query ($query), $num =mysql_num_rows ($result), if ($num > $limit) {$query = " Select ' id ' from ' News ' ORDER by ' ID ' desc limit '. $limit; $result =mysql_query ($query); Mysql_data_seek ($result, $limit-1) ; $data =mysql_fetch_array ($result); $query = "Delete from ' news ' where ' id ' < ' $data [id] '"; if (mysql_query ($query)) { echo "Original in Database". $num. " Record, superfluous ". ($num-$limit). " The record was successfully deleted and is now remaining ". $limit." Record! ";}} Else{echo "Insufficient data Logging". $limit. " Reviews No need to delete! ";}? > |
Test.sql
--PhpMyAdmin SQL Dump
--Version 3.1.5-rc1
--Http://www.phpmyadmin.net
--
--Host: localhost
--Date of generation: August 19, 2010 05:47
--Server version: 5.0.18
--PHP version: 5.2.8
SET sql_mode= "No_auto_value_on_zero";
/*!40101 SET @[email protected] @CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected] @CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected] @COLLATION_CONNECTION */;
/*!40101 SET NAMES UTF8 */;
--
--Database: ' Test '
--
-- --------------------------------------------------------
--
--The structure of the table ' News '
--
?
12345 |
CREATE TABLE IF not EXISTS ' news ' (' id ' int (one) unsigned not NULL auto_increment, ' title ' varchar (in) not null,primary KEY (' ID ')) Engine=innodb DEFAULT charset=gb2312; |
PHP implementation keeps only the latest 1000 records in MySQL