该查询应该执行以下操作:
根据问题的识别号码获取问题和相关答案。 (在这种情况下,标识号称为
account_id
。对它们进行排序,以便每个问题(以及它的相应答案)按顺序排列。
查询:
SELECT *
FROM quiz_questions q
JOIN quiz_answers a
ON q.account_id = a.account_id
WHERE q.account_id = 1840979156127491
ORDER BY q.question_id
ASC LIMIT 5
quiz_questions
表和内容:
quiz_answers
表和内容:
奇怪的改变结果:
正如您可以看到question_id的排序位置,所有值都是1
但是,另一个question_id字段(如果您查看以前的数据,这些字段匹配)代表两个,就像它们应该的那样。此外,即使问题(id为2)显示在辅助question_id字段中,问题的文本仍属于问题1:(do you like waffles
)与(do you like pancakes
)相关联。 / p>
导致这些结果的原因是什么?以下是预期结果的样子:(忽略重复字段)。
+------------------+-------------+----------------------+-----------+----------------------+---------+
| account_id | question_id | question | answer_id | answer | correct |
+------------------+-------------+----------------------+-----------+----------------------+---------+
| 1840979156127491 | 1 | Do you like waffles | 1 | Yes i like waffles | 1 |
| 1840979156127491 | 1 | Do you like waffles | 5 | I kinda like waffles | 1 |
| 1840979156127491 | 1 | Do you like waffles | 6 | Not at all | 0 |
| 1840979156127491 | 2 | Do you like pancakes | 7 | Yes | 1 |
| 1840979156127491 | 2 | Do you like pancakes | 8 | No | 0 |
+------------------+-------------+----------------------+-----------+----------------------+---------+
答案 0 :(得分:2)
您需要account_id
以及question_id
SELECT * FROM `quiz_questions`
INNER JOIN `quiz_answers`
ON `quiz_questions`.`account_id` = `quiz_answers`.`account_id`
AND `quiz_questions`.`question_id` = `quiz_answers`.`question_id`
WHERE `quiz_questions`.`account_id` = '1840979156127491'
ORDER BY `quiz_questions`.`question_id`
ASC LIMIT 5