php - 如何在 mysql/php 中向我的插入查询添加序号。不是 auto_increment

标签 php mysql

我想在每次用户创建考试时插入一个连续的项目编号。 每次他创建考试时,它都会重新启动到 1。我非常需要你们的帮助,各位。更多的权力。 :)

这是我的代码:

$examid = $_SESSION['examid'];
$itemnum = 1;

$sql = mysql_query("INSERT INTO exam_questions (question_description, question_type, question_exam_id) VALUES ('$question', '$type', '$examid')")or die(mysql_error());
$lastinsertid = mysql_insert_id();
mysql_query("UPDATE exam_questions SET question_set_id='<??????????>' WHERE question_id='$lastinsertid' LIMIT 1")or die(mysql_error());

最佳答案

尝试通过连接来实现:

UPDATE exam_questions eq cross join
       (select max(question_set_id) as qsi
        from exam_questions
        where question_id='$lastinsertid'
       ) as const
    SET eq.question_set_id = const.qsi + 1
    WHERE question_id='$lastinsertid';

关于php - 如何在 mysql/php 中向我的插入查询添加序号。不是 auto_increment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18288547/

相关文章:

mysql - 如何在mysql中的2列之间有2路唯一约束

mysql - 使用 ssl 连接到 MySQL 服务器

mysql - 如何解决子查询返回多于 1 行的问题

php - 使用 PHP CLI 通过 imap 和 mysql 或其他语言处理电子邮件检索

php - file_get_contents() 的更快替代方案

php - MYSQL 错误 1096 - 未使用表 - CODEIGNITER

php - 查询多个具有main id的表以匹配其他表的entry_id

php - 如何使用 Zend 服务亚马逊?

php - Laravel 数据库查询返回空集合

mysql - 使用 Select 在特定列上插入,然后在其他列上使用普通结构