Mysql,重置id的

时间:2010-08-02 16:09:58

标签: mysql xml

嘿伙计们,看看这个,也许你可以帮助我。 这段代码非常自我解释,它通过cron job重复自己。

<?php
$dbhost = 'blabla';
$dbuser = 'blabla';
$dbpass = 'blabla';
$dbname = 'blabla';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$table = 'cron';
$feed = 'a big xml feed';

$xml = simplexml_load_file($feed);

mysql_query("TRUNCATE TABLE ".$table.""); // empties cron 

foreach( $xml->performerinfo as $performerinfo )
{
    bla bla inside foreach to get data from xml  

    mysql_query("INSERT INTO ".$table." (performerid, category, subcategory, build, hairlength, ethnicity, willingness, pic0, pic1, pic2, pic3, pic4, age) VALUES ('$performerid', '$category', '$subcategory', '$build', '$hairlength', '$ethnicity', '$willingness', '$pic0', '$pic1', '$pic2', '$pic3', '$pic4', '$age')");  
}  // inserts into cron 

   mysql_query("TRUNCATE TABLE newtable"); // empties newtable

   mysql_query ("INSERT INTO newtable SELECT * FROM ".$table." WHERE ethnicity = 'White' AND age BETWEEN '30' AND '45' AND build <> 'above average' AND build <> 'large' AND build <> 'average' AND hairlength <> 'short'"); // inserts into newrtable

?>

问题,希望你能理解我;)

Table Cron的id为1到1000

id      int(11)             No          auto_increment

当我过滤Cron并将行插入到newtable中时,我会得到id:20,65,145,699等。

我如何重置新表的ID,使它们连续出现,从1到35,让我们说...... 强文

2 个答案:

答案 0 :(得分:2)

可以重置自动增量ID,但不会自动发生这种情况的原因是为了防止新记录具有与旧的已删除ID相同的ID。这被认为是不好的做法。

ID是39还是293440对您的申请无关紧要。如果它确实重要,那么您就会遇到架构问题。

答案 1 :(得分:1)

请勿在“select * from ...”中选择ID,而是在newtable中添加auto_increment字段。

我不确定截断是否会重置auto_increment计数器,但您可以手动执行

ALTER TABLE newtable AUTO_INCREMENT=1

您可以考虑使用临时表,因为您会一直截断它们的结尾填充。