我希望我的网站访问者仅尝试一次测验。我有两个表:questions
和 attempted_students
。
questions
包含测试的所有问题。
attempted_students
包含尝试过测试的学生详细信息及其相应的分数。虽然 id
字段是主键,会自动防止进一步重复插入,但它是有可能学生仍然可以参加考试,只有当他提交考试时才会被阻止。
因此,如果 attempted_students
表中存在他的 ID,我想在开始时向他显示一条消息“您已参加测试”。
我的方法
为了通过将查询减少到1来提高性能,我想像这样组合查询
select if((select count(id) from attempted_students where id=1)>0,(select * from questions),'');
然后我正在使用
if(!empty($rows))
//echo rows
else
//sorry you have taken the test
但是上面的查询不起作用。因此需要帮助来纠正它。其次,请告诉我,与单独执行两个查询相比,我的方法是否真的能提高性能?
最佳答案
我认为你过度优化了。将这样的条件放入数据库中对加快执行速度没有帮助(它们是子查询,而且速度很慢)。
只需在代码中处理即可。
select * from attempted_students where id=1
if(empty($rows)){
select * from questions
echo(rows)
} else {
//sorry you have taken the test
}
关于php - 当另一个查询成功时执行另一个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21526961/