将两个查询合并为一个是否有效?有什么优点或缺点? 有人可以帮我把这两个问题结合起来:
SELECT quiz_id from questions WHERE question = 10
SELECT quiz_number FROM quizzes WHERE quiz_id = 5
quiz_id与问题有1对多的关系。因此,对于一个特定问题,将只有一个quiz_id,但一个quiz_id可能最多有20个问题。
答案 0 :(得分:2)
不确定 combine 的含义,或许你的意思是subselect / subquery?在这种情况下,尝试:
--- IN syntax
SELECT quiz_number FROM quizzes WHERE quiz_id IN
(SELECT quiz_id FROM questions WHERE question = 10);
--- = syntax. This will work only if the subquery returns one value
SELECT quiz_number FROM quizzes WHERE quiz_id =
(SELECT quiz_id FROM questions WHERE question = 10);
答案 1 :(得分:1)
假设您发布的2个查询的关系,以及第二个查询中的值5是第一个查询的结果:
SELECT quiz_number FROM quizzes qz
INNER JOIN questions qu on qu.quiz_id = qz.quiz_id
WHERE question = 10
答案 2 :(得分:0)
你可以使用别名
SELECT question.quiz_id, quiz.quiz_number from questions question,quizzes quiz WHERE question.question = 10 and quiz.quiz_id =5