mysql 重新使用自动递增 id

mysql re-use auto increment id

我有以下情况,我简化了 table 以保持这里的可读性。

Table结构:(注:idauto-increment

列: id,text,rid

根据表格的输入,正在将 2 行插入 table。

INSERT INTO tablename (id,text,rid) 
VALUES (NULL, $usertext1, ??),(NULL, $usertext2, ??)

现在,我的问题在哪里?

我要第一个??第二个??相同 int,两者都需要是第一个插入的 auto-increment id!我试过 LAST_INSERT_ID() 但它返回 0.

我想要的结果:

1 |文字 | 1

2 |啦啦啦 | 1

3 |法乔夫 | 2

4 | oijgoi | 2

任何帮助将不胜感激:)

谢谢。

你必须按照这样的步骤来完成它:

<?php 
$i=0;
$firstInsertId=0;
foreach($post as $value){// suppose each $post index contains the one complete post data
    $usertxt=$value['text'];
$sql="INSERT INTO tablename (id,text,rid) 
VALUES (NULL, $usertxt,$firstInsertId)";
$mysqli->query(sql);
if($i == 0)//flag to get first inserted Id
    $firstInsertId=$mysqli->insert_id;

  $i++;
}

$sql="update tablename set rid=$firstInsertId where id=$firstInsertId";
$mysqli->query(sql);