我有三個表 - tblpollquestions,tblpollanswers和tblpollresponses。mysql限制連接 - 是否有更高效的方法來執行此操作?
我想選擇一個用戶還沒有回答的隨機問題,並帶有相應的答案。
下面的SQL返回正是我所需要的,但我擔心它需要三個SELECT來做到這一點。肯定有更有效的方法嗎?
SELECT
poll.id,
poll.question,
a.answer
FROM tblpollquestions poll
INNER JOIN tblpollanswers a ON a.question_id = poll.id
INNER JOIN (
SELECT id FROM tblpollquestions WHERE id NOT IN(
SELECT question_id FROM tblpollresponses WHERE user_id = 1
) ORDER BY RAND() LIMIT 1
) as t ON t.id = poll.id
對我來說很好。它如何執行?解釋計劃說什麼? – Randy