Question

I'm trying to do this query in Code Igniter:

Select 
    items_mtoedifsing.Marca,
    items_mtoedifsing.Modelo,
    items_mtoedifsing.Familia,
    items_movimientos.id_item,
    items_movimientos.Tipo_Movimiento,
    items_movimientos.Fecha_Movimiento,
    items_movimientos.Destino   
from items_movimientos
JOIN items_mtoedifsing ON items_mtoedifsing.id_item=items_movimientos.id_item
where 
        items_movimientos.fecha_movimiento = (Select max(fecha_movimiento) from items_movimientos it where it.id_item=items_movimientos.id_item) 
        and items_movimientos.Destino='value'

But I'm having some issues with the subquery. This is my code that return 0 rows when itshould return 1 row:

$this->db->select('*');
$this->db->from('items_movimientos');
$this->db->join('items_mtoedifsing', 'items_mtoedifsing.id_item = items_movimientos.id_item');
$this->db->where('items_movimientos.Fecha_Movimiento', '(Select max(fecha_movimiento) from items_movimientos it where it.id_item=items_movimientos.id_item)');
$this->db->where('items_movimientos.Destino', $param);

What can I do with this part:

$this->db->where('items_movimientos.Fecha_Movimiento', '(Select max(fecha_movimiento) from items_movimientos it where it.id_item=items_movimientos.id_item)');
Was it helpful?

Solution

Using active record you can do so,by passing second parameter as null and third parameter as FALSE.

$subquery="(Select max(fecha_movimiento) 
           from items_movimientos it 
           where it.id_item=items_movimientos.id_item)";
$this->db->where('items_movimientos.Fecha_Movimiento ='.$subquery, null,FALSE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top