title: PHP/MySQL: Getting Multiple Columns With the Same Name in Join Query Without Aliases?
body: <p>I have two tables. One for users and one for posts.
  The users table has the following fields: </p>
  
  <pre><code>id, username, password, created_at, modified_at
  </code></pre>
  
  <p>The posts table has the following fields:</p>
  
  <pre><code>id, user_id, title, body, created_at, modified_at
  </code></pre>
  
  <p>When I use a query like:</p>
  
  <pre><code>SELECT * FROM `users` LEFT OUTER JOIN `posts` ON users.id=posts.user_id
  </code></pre>
  
  <p>And fetch the results using PDO:</p>
  
  <pre><code>$sth = $this-&gt;$default_connection-&gt;prepare($query);
  $sth-&gt;execute();
  $sth-&gt;fetchAll(PDO::FETCH_ASSOC);
  </code></pre>
  
  <p>The returned array overwrites all the columns with the same names like <code>id, created_at, modified_at</code> like this:</p>
  
  <pre><code> Array
  (
      [0] =&gt; Array
          (
              [id] =&gt; 1
              [username] =&gt; johnSmith
              [password] =&gt; 2348r7edf8s79aa0230
              [created_at] =&gt; 0000-00-00 00:00:00
              [modified_at] =&gt; 0000-00-00 00:00:00
              [user_id] =&gt; 18
              [title] =&gt; First Post
              [body] =&gt; Just testing...
          )
  )
  </code></pre>
  
  <p>The <code>id</code> field for the user is not shown, instead overwritten by the <code>id</code> of the post. Same goes for the created_at &amp; modified_at fields. </p>
  
  <p>I believe I can solve this problem by either using aliases or something like this:</p>
  
  <pre><code>    SELECT 
         users.id, 
         users.username, 
         users.password, 
         users.created_at, 
         users.modified_at, 
         posts.id AS postId, 
         posts.user_id, 
         posts.title, 
         posts.created_at AS postCreatedAt, 
         posts.modified_at AS postModifiedAt
     FROM `users`
         LEFT OUTER JOIN `posts` ON (users.id=postId)
  </code></pre>
  
  <p>Or something similar, but I was wondering if there was a way to do this without manually writing out the name of every single column or an alias for every time I want to distinguish between two columns of the same name? 
  Is it possible to do something like:</p>
  
  <pre><code>SELECT users.* AS User and posts.* AS Post from `users` LEFT OUTER JOIN ON (User.id=Post.user_id)
  </code></pre>
  
  <p><strong>And have it automatically use aliases for every column?</strong>
  Or is there any other faster/more convenient way to do this?
  <strong>Or can I do this without having to define any aliases myself?</strong></p>
  
  <p>Thank you</p>