[英]How to retrieve few random rows of left table and all the related rows from right table in 1 to n relationship
I have Two tables . 我有两张桌子。 One is Question with following properties:
一种是具有以下属性的问题:
and other is Answer with: 另一个是答案:
Note: Question
and Answer
have 1 to n relation 注意:
Question
与Answer
存在1到n的关系
Now I need to select 10 random questions from question table and get all the rows of answers using join. 现在,我需要从问题表中选择10个随机问题,并使用join获取所有答案行。 I am new to sub query.
我是子查询的新手。 :(
:(
A subquery is definitely the way to go: 子查询绝对是必经之路:
SELECT
q.questionID,
q.question_Description,
q.weight,
q.NoOfOption
FROM
(SELECT QuestionID, Question_Description, Weight, NoOfOption FROM Question ORDER BY RAND() LIMIT 0,10) as q
LEFT OUTER JOIN Answers a
ON q.questionid = a.questionid
The subquery here just grabs 10 random questions. 这里的子查询仅捕获10个随机问题。 Then we LEFT OUTER JOIN that over to Answers.
然后我们将OUTER JOIN移至Answers。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.