表:问题
|齐德 |问题|
|- 1 -|--- q1 ----|
|- 2 -|--- q2 ----|
|- 3 -|--- q3 ----|
|- 4 -|--- q4 ----|
|- 5 -|--- q5 ----|
|- 6 -|--- q6 ----|
|-----------------|
表格:答案
|------------------------------------------------|
|答案 ID |问题 ID |回答|
|----- 1-----|----- q1------|--- a1---|
|----- 2-----|----- q1------|--- a2---|
|----- 3-----|----- q1------|--- a3---|
|----- 4-----|----- q2------|--- a4---|
|----- 5-----|----- q2------|--- a5---|
|----- 6-----|----- q2------|--- a6---|
|----- 7-----|----- q3------|--- a7---|
|----- 8-----|----- q3------|--- a8---|
|----- 9-----|----- q4-----|--- a9---|
|------------------------------------------------|
我要分页,我需要将我的问题限制为每页 1 个。
我尝试设置一个限制,但它只给我输出 1 个问题和 1 个答案,我根据问题 1 有 3 个答案
SELECT question, answer
FROM questions
LEFT JOIN `answers` ON answers.question_id = questions.question_id
LIMIT 1
输出:
|---------------------|
|问题|回答|
|---- q1----|-- a1--- |
|--------------------------------|
我如何对我的问题表设置限制,但答案表不会影响 LIMIT
您可以尝试将问题 ID 传递给查询,如下所示
SELECT question, answer
FROM questions
LEFT JOIN `answers` ON answers.question_id = questions.question_id
WHERE questions.question_id = "{DESIRED_QUESTION_ID}"
通过SQL和分页解决的替代方法是
SELECT question, answer
FROM `answers` where answers.question_id in (
select questions.question_id from `questions` LIMIT 1 OFFSET 0
)
这里的offset可以作为你从0开始的页码