title: MYSQL Subquery Rows as Columns in Main Query
body: <p>In our database we have a user table, and key-value tables for other data. We have been trying to come up with a query that will join the two, taking the keys from the k-v table as column headings, and the values as fields.</p>
  
  <p>Our only solution as of now is to <code>GROUP_CONCAT</code> the key-value pairs for each user as a column, and then parse them after the query has been output—slow and bad...</p>
  
  <p>Here is the general setup:</p>
  
  <p>User.db Tables:</p>
  
  <pre><code>------------------------------
  | uid | firstname | lastname |
  ------------------------------
  | 01 | john       | doe      |
  | 02 | jane       | doe      |
  ------------------------------
  
  -----------------------------
  | uid | question  |  answer |
  -----------------------------
  |  01 | question1 | answer1 |
  |  01 | question2 | answer2 |
  |  02 | question1 | answer3 |
  |  02 | question2 | answer4 |
  -----------------------------
  </code></pre>
  
  <p>The query result we'd like to get:</p>
  
  <pre><code>------------------------------------------------------
  | uid | firstname | lastname | question1 | question2 |
  ------------------------------------------------------
  |  01 | john      | doe      | answer1   | answer2   |
  |  02 | jane      | doe      | answer3   | answer4   |
  ------------------------------------------------------
  </code></pre>
  
  <p>I'm hoping there's a straightforward way to do this, but haven't been able to find anything. All help will be greatly appreciated.</p>