简体   繁体   English

如何在mysql / php ps中向我的插入查询添加序列号。 不自动递增

[英]How to add a sequencial number to my insert query in mysql/php ps. not auto increment

I would like to insert a sequencial item number when everytime the user creates an exam. 我想在用户每次创建考试时插入一个序列号。 everytime he creates an exam it restarts to 1. I extremely need your help, people. 每次他创建考试时,考试都会重新开始。1.人们,我非常需要您的帮助。 More power. 更大的力量。 :) :)

here is my code: 这是我的代码:

$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());

Try doing this with a join: 尝试通过联接执行此操作:

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';

You could try something like this: 您可以尝试这样的事情:

SELECT COUNT(`id`) INTO @tmpvar FROM `exam_questions` WHERE `question_exam_id`='$examid';
INSERT INTO `exam_questions`
    (`question_description`,`question_type`,`question_exam_id`,`question_id`)
    VALUES ('$question','$type','$examid',@tmpvar+1);

Note that those will have to be sent as separate mysql_query calls, but it should work nicely. 请注意,这些将必须作为单独的mysql_query调用发送,但是应该可以正常工作。 I'm also assuming that you've correctly escaped your parameters. 我还假设您已经正确地转义了参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM