I m trying to learn best practices with multiple SQL queries in PHP and mySQL. Not sure if I should have two separate queries or if I should join (or UNION?) my queries into one.
www.un.org/Depts/DGACM/index_spanish.htm 我能做以下工作,还是有另一种方式来完成同样的事情?
必须指出,我的第一个问询是把一个问答清单拉开。 第二点是把一份记分清单,根据用户的立分数向用户分配。 这两张表格之间的唯一关系是,这两张表格都使用与我“quiz”表中的ID有联系的同一外国钥匙。 我不需要日本海洋研究网旁边的这些表格,但我认为,我或许需要对这些表格进行联合国讨论,因此我的第二个询问结果在第一次之后出现。
现在,如果我把我的问答放在一起,我不知道我会如何通过问答结果来建立我的阵列。 当我得出每一项结果时,我需要某种有条件的逻辑,这种逻辑与我的第一份询问结果和我的第二次询问结果有所不同。 我如何写这些条件?
Here is what I m doing now that seems to work fine as two separate queries...
. 首次对数据库进行排序,以便了解我的问答:
$result_quiz = mysql_query("
SELECT quiz_question.question_text, quiz_question.id, quiz_answer.answer_text, quiz_answer.points
FROM quiz
JOIN quiz_question ON (quiz.id = quiz_question.quiz_id)
JOIN quiz_answer ON (quiz_question.id = quiz_answer.quiz_question_id)
WHERE quiz.id = $id;
");
...then based on the above query,building my range of questions and questions:
$quiz = array();
while ($row = mysql_fetch_assoc($result_quiz)) {
if (!isset($quiz[ questions ][ q .$row[ id ]. ])) {
$quiz[ questions ][ q .$row[ id ]. ] = array(
question => $row[ question_text ],
answers => array()
);
}
$quiz[ questions ][ q .$row[ id ]. ][ answers ][] = array( answer => $row[ answer_text ], points => $row[ points ]);
}
...then query the database for second summary:
$result_profile = mysql_query("
SELECT quiz_profile.name, quiz_profile.description, quiz_profile.min_points, quiz_profile.max_points
FROM quiz
JOIN quiz_profile ON (quiz.id = quiz_profile.quiz_id)
WHERE quiz.id = $id;
");
...then loop through my second query so, I can append the score profiles to my range:
while ($row = mysql_fetch_assoc($result_profile)) {
$quiz[ profiles ][] = array( name => $row[ name ], description => $row[ description ], min_points => $row[ min_points ], max_points => $row[ max_points ]);
}
www.un.org/Depts/DGACM/index_spanish.htm 这是最佳办法吗?
www.un.org/Depts/DGACM/index_spanish.htm 或者,我是否把问询结合到一个使用联合国意见?。
If so, how can I write my conditions to know what results I m looping through?
感谢SO!