CodeIgniter Forums
Can anyone help me with this one. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: Can anyone help me with this one. (/showthread.php?tid=77821)



Can anyone help me with this one. - gheesixz - 10-22-2020

http://sqlfiddle.com/#!9/a9ec7e/4/0

Code:
SELECT
        p.*
    FROM
        (
          -- For every product_id, find maximum created_at time
          SELECT
              product_id, max(created_at) AS created_at
          FROM
              purchases
          GROUP BY
              product_id
         ) AS mx
         -- JOIN to the whole table
         JOIN purchases p ON
              mx.product_id = p.product_id AND mx.created_at = p.created_at
    ORDER BY
         product_id ;

Thats exactly what i need for my project.
But i can't / dont know how to do this query in codeigniter.

Im using CodeIgniter 3.


RE: Can anyone help me with this one. - ElTomTom - 10-22-2020

Fast Option:

PHP Code:
$query $this->db->query('YOUR QUERY HERE'); 

Or maybe create a view (without order) and use like a table.


RE: Can anyone help me with this one. - gheesixz - 10-23-2020

(10-22-2020, 05:52 AM)ElTomTom Wrote: Fast Option:

PHP Code:
$query $this->db->query('YOUR QUERY HERE'); 

Or maybe create a view (without order) and use like a table.
Hi. Thanks for your answer. Yup. I solved it already using a variable $query and then return $query->result();

Thank you.