title: group_concat with multiple joins in MySQL
body: <h2>Database schema</h2>
  
  <pre><code>create table `questions` (
    `id` int not null auto_increment,
    `title` varchar(45) not null,
    primary key (`id`));
  
  create table `tags` (
    `id` int not null auto_increment,
    `question_id` int not null,
    `name` varchar(45) not null,
    primary key (`id`));
  
  create table `comments` (
    `id` int not null auto_increment,
    `question_id` int not null,
    `body` varchar(45) not null,
    primary key (`id`));
  
  insert into questions (title) values
  ("title1"), ("title2"), ("title3");
  
  insert into tags (question_id, name) values
  (1, "javascript"), (1, "php"), (1, "c#"), (2, "mysql"), (2, "php"), (3, "c#");
  
  insert into comments (question_id, body) values
  (1, "comment1"), (1, "comment1"), (1, "comment2"), (3, "comment3");
  </code></pre>
  
  <p>That's how it looks visually:</p>
  
  <h3><code>questions</code> table</h3>
  
  <pre><code>| id |  title |
  |----|--------|
  |  1 | title1 |
  |  2 | title2 |
  |  3 | title3 |
  </code></pre>
  
  <h3><code>tags</code> table</h3>
  
  <pre><code>| id | question_id |       name |
  |----|-------------|------------|
  |  1 |           1 | javascript |
  |  2 |           1 |        php |
  |  3 |           1 |         c# |
  |  4 |           2 |      mysql |
  |  5 |           2 |        php |
  |  6 |           3 |         c# |
  </code></pre>
  
  <h3><code>comments</code> table</h3>
  
  <pre><code>| id | question_id |     body |
  |----|-------------|----------|
  |  1 |           1 | comment1 |
  |  2 |           1 | comment1 |
  |  3 |           1 | comment2 |
  |  4 |           3 | comment3 |
  </code></pre>
  
  <p>Each question has to have at least one tag. It can also have 0 or more comments. There can be two comments with the same body on one question.</p>
  
  <h2>Desired output</h2>
  
  <p>I want to select all questions, that is, their ids, titles, tags and comments.</p>
  
  <p>The output should look like that:</p>
  
  <pre><code>| id |  title |       tags        |          comments          |
  |----|--------|-------------------|----------------------------|
  |  1 | title1 | c#,php,javascript | comment1,comment1,comment2 |
  |  2 | title2 | php,mysql         | (null)                     |
  |  3 | title3 | c#                | comment3                   |
  </code></pre>
  
  <h2>Attempts to solve the problem</h2>
  
  <p>I tried the following query:</p>
  
  <pre><code>select questions.id, questions.title,
    group_concat(tags.name), group_concat(comments.body)
  from questions
  join tags on questions.id = tags.question_id
  left join comments on questions.id = comments.question_id
  group by questions.id
  </code></pre>
  
  <p>Unfortunately, it doesn't work as expected. It produces the following output:</p>
  
  <pre><code>| id |  title | group_concat(distinct tags.name) |                                                      group_concat(comments.body) |
  |----|--------|----------------------------------|----------------------------------------------------------------------------------|
  |  1 | title1 |                c#,php,javascript | comment1,comment1,comment1,comment2,comment2,comment2,comment1,comment1,comment1 |
  |  2 | title2 |                        php,mysql |                                                                           (null) |
  |  3 | title3 |                               c# |                                                                         comment3 |
  </code></pre>
  
  <p>As you see, for the first question I get each comment three times, because there are three tags on this question.</p>
  
  <p>Also, the comments are in the wrong order. They should be in the same order as they were inserted, that is, <code>comment1,comment1,comment2</code>, not <code>comment1,comment2,comment1</code>.</p>
  
  <p>I can't use <code>distinct</code> for comments, as there can be multiple comments with the same body on one question.</p>
  
  <p>I know that this probably could be solved with nested <code>select</code>s, but as far as I know it would have a huge negative impact on the performance of the query.</p>
  
  <h2>SQL Fiddle</h2>
  
  <p><a href="http://sqlfiddle.com/#!9/429ad7/6/0" rel="noreferrer">The SQL Fiddle</a> with database schema and my query.</p>