title: Tagging in php and sql
body: <p>I have the following tables in my database:</p>
  
  <p>thread --- thread_replies --- replies
  thread --- thread_tags --- tags</p>
  
  <pre><code>thread: id, title, content, created, author_id
  
  thread_tags: id, thread_id, tag_id
  
  tags: id, name
  </code></pre>
  
  <p>I can select tags for a certain thread using the following:</p>
  
  <pre><code>   SELECT GROUP_CONCAT(DISTINCT tags.name ORDER BY tags.name DESC SEPARATOR ',') AS tags
      FROM tags INNER JOIN thread_tags ON tag_id = tags.id
      INNER JOIN thread ON thread_id = thread.id
      WHERE thread.id = 20
  </code></pre>
  
  <p>However what I want to do is when I select all the threads, I want to select all the tags associated with each thread as well...</p>
  
  <p>I want to build a multidimensional array like the following:</p>
  
  <pre><code>    Array
  (
      [t_id] =&gt; 20
      [title] =&gt; this is a title
      [content] =&gt; This is content
      [username] =&gt; username1
      [a_id] =&gt; 35
      [tags] =&gt; tag1, tag2, tag3
  )
  </code></pre>
  
  <p>I want to know what sql statement I can use to create this for each thread?</p>
  
  <p>So if I select all the threads and their associated authors, how can I also get the tags associated with each thread?</p>
  
  <p>Currently I use this to get each thread and its author:</p>
  
  <pre><code>SELECT thread.id AS t_id, thread.title,
                           thread.content, author.username, author.id AS a_id
                           FROM thread INNER JOIN author ON thread.author_id = author.id 
  </code></pre>