0

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();

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

$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')
  -> orderBy('h.field_cg_h_value', 'ASC')
  -> orderBy('g.field_cg_g_value', 'ASC');

1 Answer 1

1

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 -> 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')
  ->orderBy('g.entity_id', 'DESC');
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.