Skip to main content
1 of 3
svetlio
  • 1.5k
  • 15
  • 19

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 -> join('field_data_field_cg_h', 'h', 'w.entity_id = h.entity_id');
$query -> fields('h', array('entity_id', 'field_cg_h_value'));

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

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

$records = $query->execute();
svetlio
  • 1.5k
  • 15
  • 19