If I am GROUP BYin a unique way and apply the sentence LIMITto the query, will all groups be calculated before the limit is applied?
If I have one hundred records in a table (each has a unique key), will I have 100records in a temporary table created (for GROUP BY) before it is applied LIMIT?
Case study why I need it:
Take Stack Overflowfor example.
Each query that you run to display a list of questions also shows the user who asked this question and the number of icons that he has.
So, while the user β question is one to one, the user β icons have a lot.
The only way to do this in one query (and not in a question, and another in relation to users and then combine the results) is to group the query using the primary key (question_id) and join + group_concat into the user_badges table.
The same goes for TAGS questions.
Code example:
Table Questions:
question_id (int)(pk)| question_body(varchar)
Table tag-question:
question-id (int) | tag_id (int)
SELECT:
SELECT quesuestions.question_id,
questions.question_body,
GROUP-CONCAT(tag_id,' ') AS 'tags-ids'
FROM
questions
JOIN
tag_question
ON
questions.question_id=tag-question.question-id
GROUP BY
questions.question-id
LIMIT 15
source
share