title: Sub-query in Knex
body: <p>I'm looking to essentially make this sort of query in Knex, but I can't quite get it to work: </p>
  
  <pre class="lang-sql prettyprint-override"><code>select distinct *
  from
  (
    select *, 1 as rank from table1 where Word like 'mike'
    union
    select *, 2 as rank from table1 where Word like 'mike%'
    union
    select *, 3 as rank from table1 where Word like '%mike%'
  ) as X
  order by WordOrder
  </code></pre>
  
  <p>I noticed a similar issue <a href="https://github.com/tgriesser/knex/issues/27">here</a> and tried to follow their advice, but can't seem to spot my bug (or if this is even the proper way of doing this in the first place).</p>
  
  <pre class="lang-js prettyprint-override"><code>var q = DB.knex('Users').select("*", "1 as rank").where("User", "like", query).
      union(function() {
          this.select("*", "2 as rank").where("User", "like", query + "%")
      }).
      union(function() {
          this.select("*", "3 as rank").where("User", "like", query + "%")
      });
  
  DB.knex("Users").distinct("*").from('(' + q.toString() + ') as X').
      orderBy('rank').select().then(...)
  </code></pre>
  
  <p>If it's any help, that particular query generates the following error: </p>
  
  <pre><code>Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1`` order by `rank` asc' at line 1, sql: select distinct * from `select` as ``1`` order by `rank` asc, bindings: 
  </code></pre>