title: MySQL - Rows to Columns
body: <p>I tried to search posts, but I only found solutions for SQL Server/Access. I need a solution in MySQL (5.X).</p>
  <p>I have a table (called history) with 3 columns: hostid, itemname, itemvalue.<br />
  If I do a select (<code>select * from history</code>), it will return</p>
  <pre><code>   +--------+----------+-----------+
     | hostid | itemname | itemvalue |
     +--------+----------+-----------+
     |   1    |    A     |    10     |
     +--------+----------+-----------+
     |   1    |    B     |     3     |
     +--------+----------+-----------+
     |   2    |    A     |     9     |
     +--------+----------+-----------+
     |   2    |    C     |    40     |
     +--------+----------+-----------+
  </code></pre>
  <p>How do I query the database to return something like</p>
  <pre><code>   +--------+------+-----+-----+
     | hostid |   A  |  B  |  C  |
     +--------+------+-----+-----+
     |   1    |  10  |  3  |  0  |
     +--------+------+-----+-----+
     |   2    |   9  |  0  |  40 |
     +--------+------+-----+-----+
  </code></pre>