Make a SUM with codeigniter

0

I need to add the detail table data the problem esque does not contain the date if not in the main table, it made it very easy in the following way: Model:

$this->db->select('COALESCE(SUM(cajas),0) as total_cajas, COALESCE(SUM(peso),0) as total_peso, COALESCE(SUM(precio),0) as total_precio');

all right up there I could correctly return the information but ALL! the problem is that I want to bring it by date try with:

        $this->db->select('a.peso,a.cajas,a.precio, b.fecha');
        $this->db->from('separacion_detalles a');
        $this->db->join('separacion b','a.idseparacion = b.id');
        $this->db->where('b.fecha',$año.$dia);
        $result = $this->db->get();
        return $result->row();

all good too, the problem esque where I put the sum of boxes, weight, price?

    
asked by DoubleM 22.04.2018 в 09:40
source

1 answer

2

When you can not make a query in Codeigniter through its functions you can use $this->db->query($sql); just put in $sql the query you need:

    function obtener($parametros){
        $sql="COALESCE(SUM(a.cajas),0) as total_cajas, 
        COALESCE(SUM(a.peso),0) as total_peso, 
        COALESCE(SUM(a.precio),0) as total_precio
        from separacion_detalles a inner join separacion b 
        on a.idseparacion = b.id
        where b.fecha = '$año.dia'";
        $consulta = $this->db->query($sql);
        return $consulta->row_array();
    }

That way you can make any query, I hope it helps, do not complicate, greetings.

    
answered by 22.04.2018 / 14:36
source