ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Need to ORDER_BY a field in the JOINed table

March 20, 2013 5:16am

Subscribe [1]
  • #1 / Mar 20, 2013 5:16am

    poldings

    1 posts

    We want to query a table to find a list of active product (motorcycles) and then query another table to find the primary picture for each product.

    We want to take only the primary picture for each product to be used for the thumbnail image. The primary picture will be the one with the lowest numeric value in the ‘priority’ field.

    So, we’re using this ...

    $this->db->select('*');
    $this->db->from('product');
    $this->db->where('product.status', 'active');
    
    $this->db->limit($limit, $offset); // used for pagination
    
    $this->db->order_by('product.make asc');
    $this->db->order_by('product.model asc');
    
    $this->db->join('product_images', 'product_images.product_id = product.product_id');
    $this->db->order_by('product_images.priority asc');
    
    $this->db->group_by('product.product_id');
    
    $ret['rows'] = $this->db->get()->result();

    It all works okay except the picture selected is not actually sorted by priority so basically, this line is not working ...

    $this->db->order_by('product_images.priority asc');

    Any thoughts as to how we can sort the table that is joined?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases