title: Select dynamic Columns in mysql
body: <p>Is it possible to traverse a table like this:</p>
  
  <pre>
  mysql> select * from `stackoverflow`.`Results`;
  +--------------+---------+-------------+--------+
  | ID           | TYPE    | CRITERIA_ID | RESULT |
  +--------------+---------+-------------+--------+
  |            1 | car     | env         | 1      |
  |            2 | car     | gas         |        |
  |            3 | car     | age         |        |
  |            4 | bike    | env         | 1      |
  |            5 | bike    | gas         |        |
  |            6 | bike    | age         | 1      |
  |            7 | bus     | env         | 1      |
  |            8 | bus     | gas         | 1      |
  |            9 | bus     | age         | 1      |
  +--------------+---------+-------------+--------+
  9 rows in set (0.00 sec)
  </pre>
  
  <p>Into this:</p>
  
  <pre>
  +------+-----+-----+-----+
  | TYPE | env | gas | age |
  +------+-----+-----+-----+
  | car  | 1   |     |     |
  | bike | 1   |     | 1   |
  | bus  | 1   | 1   | 1   |
  +------+-----+-----+-----+
  </pre>
  
  <p>The aim is to select all the <code>CRITERIA_ID</code>s and use them as a column.
  As rows i like to use all the <code>TYPE</code>s .</p>
  
  <ul>
  <li>All Criterias: <code>SELECT distinct(CRITERIA_ID) FROM stackoverflow.Results;</code> </li>
  <li>All Types <code>SELECT distinct(TYPE) FROM stackoverflow.Results;</code></li>
  </ul>
  
  <p>But how combine them into a view or smth. like this?</p>
  
  <p>If you like to play with the data. This is a script to generate the table:</p>
  
  <pre><code>CREATE SCHEMA `stackoverflow`;
  CREATE TABLE `stackoverflow`.`Results` (
    `ID` bigint(20) NOT NULL AUTO_INCREMENT,
    `TYPE` varchar(50) NOT NULL,
    `CRITERIA_ID` varchar(5) NOT NULL,
    `RESULT` bit(1) NOT NULL,
    PRIMARY KEY (`ID`)
  ) 
  ENGINE=InnoDB;
  
  INSERT INTO `stackoverflow`.`Results`
  (
   `ID`,
   `TYPE`,
   `CRITERIA_ID`,
   `RESULT`
  )
  VALUES
  ( 1, "car", env, true ),
  ( 2, "car", gas, false ),
  ( 3, "car", age, false ),
  ( 4, "bike", env, true ),
  ( 5, "bike", gas, false ),
  ( 6, "bike", age, true ),
  ( 7, "bus", env, true ),
  ( 8, "bus", gas, true ),
  ( 9, "bus", age, true );
  </code></pre>