How to orderBy joined table after ordered main in db_select I have 3 joined tables - The query search some string on main table then join other by entity_id. I want to orderBy records in order first second third tables result, but no success now. At this time I have order by only for main table.

My query:

$query = db_select('field_data_field_cg_w', 'w');

$query -&gt; join('field_data_field_cg_h', 'h', 'w.entity_id = h.entity_id');
$query -&gt; fields('h', array('entity_id', 'field_cg_h_value'));

$query -&gt; join('field_data_field_cg_g', 'g', 'w.entity_id = g.entity_id');
$query -&gt; fields('g', array('entity_id', 'field_cg_g_value'));

$query -&gt; fields('w', array('entity_id', 'field_cg_w_value'))
  -&gt; condition('w.field_cg_w_value', $options['string'] . '%', 'LIKE')
  -&gt; orderBy('w.field_cg_w_value', 'ASC');

$records = $query-&gt;execute();


EDIT:
The result (last part of query) after Clive's answer:

$query -&gt; fields('w', array('entity_id', 'field_cg_w_value'))
  -&gt; condition('w.field_cg_w_value', $options['string'] . '%', 'LIKE')
  -&gt; orderBy('w.field_cg_w_value', 'ASC')
  -&gt; orderBy('h.field_cg_h_value', 'ASC')
  -&gt; orderBy('g.field_cg_g_value', 'ASC');
Not sure I understand but you're not limited to a single orderby() in a SelectQuery so you would just add another one with the correct alias, e.g.

$query -&gt; fields('w', array('entity_id', 'field_cg_w_value'))
  -&gt;condition('w.field_cg_w_value', $options['string'] . '%', 'LIKE')
  -&gt;orderBy('w.field_cg_w_value', 'ASC')
  -&gt;orderBy('g.entity_id', 'DESC');