title: Sequel -- can I alias subqueries in a join?
body: <p>Using <a href="http://sequel.rubyforge.org/" rel="noreferrer">Sequel</a> I'd like to join two subqueries together that share some column names, and then table-qualify those columns in the select.</p>
  
  <p>I understand how to do this if the two datasets are just tables.  E.g. if I have a <code>users</code> table and an <code>items</code> table, with items belonging to users, and I want to list the items' names and their owners' names:</p>
  
  <pre><code>@db[:items].join(:users, :id =&gt; :user_id).
    select{[items__name, users__name.as(user_name)]}
  </code></pre>
  
  <p>produces</p>
  
  <pre><code>SELECT "items"."name", "users"."name" AS "user_name" 
    FROM "items" 
  INNER JOIN "users" ON ("users"."id" = "items"."user_id")
  </code></pre>
  
  <p>as desired.</p>
  
  <p>However, I'm unsure how to do this if I'm joining two arbitrary datasets representing subqueries (call them <code>my_items</code> and <code>my_users</code>)  </p>
  
  <p>The syntax would presumably take the form</p>
  
  <pre><code>my_items.join(my_users, :id =&gt; :user_id).
    select{[ ... , ... ]}
  </code></pre>
  
  <p>where I would supply qualified column names to access <code>my_users.name</code> and <code>my_items.name</code>. What's the appropriate syntax to do this?</p>
  
  <p>A partial solution is to use <code>t1__name</code> for the first argument, as it seems that the dataset supplied to a join is aliased with <code>t1</code>, <code>t2</code>, etc.  But that doesn't help me qualify the item name, which I need to supply to the second argument.</p>
  
  <p>I think the most desirable solution would enable me to provide aliases for the datasets in a join, e.g. like the following (though of course this doesn't work for a number of reasons)</p>
  
  <pre><code>my_items.as(alias1).join(my_users.as(alias2), :id =&gt; :user_id).
    select{[alias1__name, alias2__name ]}
  </code></pre>
  
  <p>Is there any way to do this?  </p>
  
  <p>Thanks!</p>
  
  <p><strong>Update</strong></p>
  
  <p>I think <code>from_self</code> gets me part of the way there, e.g.</p>
  
  <pre><code>my_items.from_self(:alias =&gt; :alias1).join(my_users, :id =&gt; :user_id).
    select{[alias1__name, t1__name]}
  </code></pre>
  
  <p>seems to do the right thing.  </p>