title: SQL JOIN : Prefix fields with table name
body: <p>I have the following tables </p>
  
  <pre><code>CREATE TABLE `constraints` (
    `id` int(11),
    `name` varchar(64),
    `type` varchar(64)
  );
  
  CREATE TABLE `groups` (
    `id` int(11),
    `name` varchar(64)
  );
  
  CREATE TABLE `constraints_to_group` (
    `groupid` int(11),
    `constraintid` int(11)
  );
  </code></pre>
  
  <p>With the following data :</p>
  
  <pre><code>INSERT INTO `groups` (`id`, `name`) VALUES
  (1, 'group1'),
  (2, 'group2');
  
  INSERT INTO `constraints` (`id`, `name`, `type`) VALUES
  (1, 'cons1', 'eq'),
  (2, 'cons2', 'inf');
  
  INSERT INTO `constraints_to_group` (`groupid`, `constraintid`) VALUES
  (1, 1),
  (1, 2),
  (2, 2);
  </code></pre>
  
  <p>I want to get all constraints for all groups, so I do the following :</p>
  
  <pre><code>SELECT groups.*, t.* FROM groups
  LEFT JOIN
      (SELECT * FROM constraints
      LEFT JOIN constraints_to_group
      ON constraints.id=constraints_to_group.constraintid) as t
  ON t.groupid=groups.id
  </code></pre>
  
  <p>And get the following result :</p>
  
  <pre><code>id|  name  | id |  name   type   groupid   constraintid   
  -----------------------------------------------------
  1 | group1 | 1  | cons1 |  eq  | 1       | 1
  1 | group1 | 2  | cons2 | inf  | 1       | 2
  2 | group2 | 2  | cons2 | inf  | 2       | 2
  </code></pre>
  
  <p><strong>What I'd like to get :</strong></p>
  
  <pre><code>group_id | group_name | cons_id |  cons_name | cons_type | groupid | constraintid   
  -------------------------------------------------------------------------------------
  1        | group1     | 1       | cons1      |  eq       | 1       | 1
  1        | group1     | 2       | cons2      | inf       | 1       | 2
  2        | group2     | 2       | cons2      | inf       | 2       | 2
  </code></pre>
  
  <p><strong>This is an example, in my real case my tables have much more columns so using the <code>SELECT groups.name as group_name, ...</code> would lead to queries very hard to maintains.</strong></p>