codeigniter select min and max product price from multy tables
04:06 09 Jan 2021

I need to select all (*) the rows from products, prices.. but I need to select the MIN and MAX price from prices. I've read up on how to do this, but how do I do this within an INNER JOIN

this is my build_query() function:

 //build sql query string
public function build_query($type = "active")
{
    $select = "products.*,

        (SELECT CONCAT(price) FROM prices WHERE products.id = prices.product_id ORDER BY created_at DESC LIMIT 1) AS price;}

and this is my products_maxmin_price() function:

public function get_shops_products_maxmin_price($parent_id)
{
    $this->build_query();
    $this->db->select('MAX(price) as p_max, MIN(price)  p_min ');
    $this->db->where('products.parent_id', clean_number($parent_id));
    return $this->db->get('products')->result();
}

and this is my view:


                         p_min; ?>
                        


                         p_max; ?>
                        
php codeigniter join aggregate-functions query-builder