我在下面有一个查询:
$questionquery = "
SELECT DISTINCT q.QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS Answer, r.ReplyType,
q.QuestionMarks, q.SessionId
FROM Answer an
INNER JOIN Question q ON q.QuestionId = an.QuestionId and an.SessionId = q.SessionId
JOIN Reply r ON q.ReplyId = r.ReplyId
JOIN Option_Table o ON q.OptionId = o.OptionId
WHERE ".implode(" AND ", array_fill(0, $numTerms, "q.QuestionContent LIKE ?"))."
GROUP BY an.SessionId, an.QuestionId
ORDER BY ".implode(", ", array_fill(0, $numTerms, "IF(q.QuestionContent LIKE ?, 1, 0) DESC"))."
";
现在下面是每个数据库表的样子:
会话表:
SessionId SessionName
1 AAA
2 AAB
3 AAC
问题表:
SessionId QuestionId QuestionContent ReplyId OptionId
1 1 Here is a single answer 2 1
2 3 Here is a single answer 2 1
3 3 Here is a single answer 2 1
答案表:
AnswerId SessionId QuestionId Answer
1 1 1 True
2 2 3 True
3 3 3 True
回复表:
ReplyId ReplyType
1 Single
2 Multiple
选项表:
OptionId OptionType
1 True or False
现在我遇到的问题是,在数据库中,我有三个完全相同的问题,并且具有相同的答案、相同的分数、相同的选项类型等,都一样,这很好。但是在查询中我想要做的是,如果有多行的列都匹配,那么只显示一次。所以目前它在下面显示为查询输出:
Here is a single answer True or False 1 True Multiple 5
Here is a single answer True or False 1 True Multiple 5
Here is a single answer True or False 1 True Multiple 5
但由于问题的所有细节都是相同的,它应该只显示一次,如下所示:
Here is a single answer True or False 1 True Multiple 5
如果有多行数据完全相同,如何让查询显示该行一次?
更新:
$questionquery = "
SELECT DISTINCT q.QuestionContent, o.OptionType, q.NoofAnswers, Answer, r.ReplyType,
q.QuestionMarks
FROM
(
SELECT DISTINCT q.QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS Answer, r.ReplyType,
q.QuestionMarks, q.SessionId
FROM Answer an
INNER JOIN Question q ON q.QuestionId = an.QuestionId and an.SessionId = q.SessionId
JOIN Reply r ON q.ReplyId = r.ReplyId
JOIN Option_Table o ON q.OptionId = o.OptionId
WHERE ".implode(" AND ", array_fill(0, $numTerms, "q.QuestionContent LIKE ?"))."
GROUP BY an.SessionId, an.QuestionId
)
AS tmpresult
ORDER BY ".implode(", ", array_fill(0, $numTerms, "IF(q.QuestionContent LIKE ?, 1, 0) DESC"))."
";