问题描述:

I have 2 tables for a quiz: "questions" and "answers". Every question has 4 answers. I wanna request 50 random question. I try this but it returned less than 50 question.

(I need all fields of all 50 questions and 200 answers).

SELECT Q.*, A.* FROM questions Q

LEFT JOIN answers A ON Q.pid = A.pid

WHERE Q.pid >= RAND() * (SELECT MAX(pid) FROM questions)

AND Q.lid IN $array

GROUP BY Q.pid

LIMIT 50

网友答案:

Try the following:

 SELECT y.* 
   FROM 
      ( SELECT * 
          FROM questions 
         ORDER 
            BY RAND() LIMIT 2
      ) x 
   JOIN answers y 
     ON y.question_id = x.question_id 
  ORDER  
     BY x.question_id;
相关阅读:
Top