如何在我的问题表上设置限制(PHP Mysql)

问题描述 投票:0回答:1

表:问题

|齐德 |问题|
|- 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--- |
|--------------------------------|

我需要这样的输出

|---------------------|
|问题|回答|
|---- 1-----|-- a1----|
|---- 1-----|-- a2----|
|---- 1-----|-- a3----|
|--------------------------------|

那么第二个问题在下一页

php mysql
1个回答
0
投票

您可以尝试将问题 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开始的页码

© www.soinside.com 2019 - 2024. All rights reserved.