I am creating a query below where a it retrieves data depending on the term(s) the user entered in the a search bar. Now what I am trying to do is not display duplicate data, so if there are 2 rows where all the fields are exactly the same, then it is a duplicate row, so it only shows this row once not multiple times. Now I think it seems to do this as I see no duplicate rows but all I did was do a GROUP BY with all the SELECT fields except for the Answer field as it doesn’t let me have a group concat in the GROUP BY clause.
But what my question is that do I need that field in the GROUP Y clause to not show duplicate rows or is it not really needed?
SELECT
q.QuestionContent,
o.OptionType,
q.NoofAnswers,
GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR ',') AS Answer,
r.ReplyType,
q.QuestionMarks
FROM Question q
LEFT JOIN Answer an
ON q.QuestionId = an.QuestionId
LEFT JOIN Reply r
ON q.ReplyId = r.ReplyId
LEFT JOIN Option_Table o
ON q.OptionId = o.OptionId
WHERE ".implode(" AND ", array_fill(0, $numTerms, "q.QuestionContent LIKE ?"))."
GROUP BY q.QuestionContent,
o.OptionType,
q.NoofAnswers,
r.ReplyType,
q.QuestionMarks
ORDER BY ".implode(", ", array_fill(0, $numTerms, "IF(q.QuestionContent LIKE ?, 1, 0) DESC"))."
No, you do not need the
group_concat()in theselect. Thegroup bywill ensure that any particular combination of values will appear once for the columns in thegroup by. Because these are guaranteed to be distinct, you don’t have to worry about any other columns.The
group_concat()is a calculated column, based on the summaries. You are not permitted to have such columns in agroup bystatement. If you want to aggregate on them again, then you need to use a subquery.