php - 如何使用 answeroption 水平浮动一行对查询结果进行排序
php - How to ordering query result with answeroption float horizontal by a line
我想按如下所示订购我的查询。 :)
Table 问题
id question
100 What is your name?
101 What is your nick name?
Table 回答
id answeroption questionid
1 John 100
2 Mike 100
3 Adam 100
4 Am 101
5 Den 101
所以,我希望结果显示如下,答案选项水平浮动一行:
id question answer1 answer2 answer3
100 What is your name? John Mike Adam
101 What is your nick name? Am Den
我使用的函数查询,但我坚持要修复我实际想要的结果。
function test($con){
$q = "select a.id, a.question, b.answeroption
from question a
inner join answer b
on a.id = b.questionid
";
$run = mysqli_query($con,$q);
$row = mysqli_fetch_array($run);
return $row;
}
您将要使用 GROUP_CONCAT 和 GROUP BY
$q = "SELECT a.id, a.question, GROUP_CONCAT(b.answeroption) AS answeroptions
FROM question a
INNER JOIN answer b ON a.id = b.questionid
GROUP BY a.id
";
我想按如下所示订购我的查询。 :)
Table 问题
id question
100 What is your name?
101 What is your nick name?
Table 回答
id answeroption questionid
1 John 100
2 Mike 100
3 Adam 100
4 Am 101
5 Den 101
所以,我希望结果显示如下,答案选项水平浮动一行:
id question answer1 answer2 answer3
100 What is your name? John Mike Adam
101 What is your nick name? Am Den
我使用的函数查询,但我坚持要修复我实际想要的结果。
function test($con){
$q = "select a.id, a.question, b.answeroption
from question a
inner join answer b
on a.id = b.questionid
";
$run = mysqli_query($con,$q);
$row = mysqli_fetch_array($run);
return $row;
}
您将要使用 GROUP_CONCAT 和 GROUP BY
$q = "SELECT a.id, a.question, GROUP_CONCAT(b.answeroption) AS answeroptions
FROM question a
INNER JOIN answer b ON a.id = b.questionid
GROUP BY a.id
";