我已经知道如何执行随机 mysql/php 查询,但是我如何跟踪已经查询过的行不再被提取。用例:有 10 个问题的测验。问题需要随机排列。我如何跟踪问题(行)2、5、6、8、9 已经得到回答,但问题 1、3、4、7、10 仍然存在。这是我到目前为止所拥有的:
$current_test = $_SESSION['testid'];
// v v v query for this TEST
$tests = mysql_query("SELECT * FROM the_tests WHERE the_tests.testid=$current_test");
$test = mysql_fetch_array($tests);
// ^ ^ ^ query for this TEST
// v v v query for the QUESTIONS from this Test
// Generate
if ($test['randomize']==1){
$offset_result = mysql_query("SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM the_questions WHERE the_questions.test_id_q=$current_test");//SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM the_questions WHERE `qid` NOT IN (1,5,6) AND the_questions.test_id_q=1
$offset_row = mysql_fetch_object($offset_result);
$offset = $offset_row->offset;
$questions = mysql_query("SELECT * FROM the_questions WHERE the_questions.test_id_q=$current_test LIMIT $offset, 1 " );
}else{
$questions = mysql_query("SELECT * FROM the_questions WHERE the_questions.test_id_q=$current_test");
}
$totalQuestions = mysql_query("SELECT * FROM the_questions WHERE the_questions.test_id_q=$current_test");
$totalQs = mysql_num_rows($totalQuestions);
$testQ = mysql_fetch_array($questions);
提前致谢!
最佳答案
您希望使用 NOT IN (5,23,45);
所以基本上保留一个包含每个随机项目 ID 值的数组,并将其提供给您的 SQL 子句。
mysql_query("SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM the_questions WHERE the_questions.test_id_q=$current_test AND the_questions.id NOT IN (".implode(',',$excluded).")");
现在,这引发了另一个问题:$excluded 数组。它应该是暂时的排除还是应该是永久的? 如果它不应该是永久性的,则将其存储在 session 中(例如: $_SESSION['excluded'] ) 如果用户 3 天后来,他可能能够重新回答该问题。
如果这必须是永久性的,那么请将已回答该问题的事实存储在专用于该问题的列中,或者只是检查是否有答案?
mysql_query("SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM the_questions WHERE the_questions.test_id_q=$current_test AND the_questions.answer ==''");
关于php - 我如何查询随机行并跟踪已查询的内容(这是重要的部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10744739/