CodeIgniter Forums
Query-string to Active Records - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Query-string to Active Records (/showthread.php?tid=56825)



Query-string to Active Records - El Forum - 01-23-2013

[eluser]Patrick Reck[/eluser]
I have this SQL Query which works fine in it self. However, I can't quite figure how to write it in Active Record. How can I do this?

Code:
SELECT
  users.*,
  users_info.*,
  mail.*
FROM
  `users`
  JOIN `mail` ON `users`.`id` = `mail`.`user_from`
  JOIN `users_info` ON `users_info`.`user_id` = `mail`.`user_from`
  JOIN (
    SELECT  user_from, MAX(date) AS date
    FROM mail
    WHERE user_to = '1'
    GROUP BY user_from
  ) most_recent ON mail.user_from = most_recent.user_from AND mail.date = most_recent.date
  WHERE `user_to` =  '1'



Query-string to Active Records - El Forum - 01-23-2013

[eluser]Otemu[/eluser]
Hi,

You can't do sub queries in active record, there a some libraries that allow you to achieve this if you google for them. Your not forced to use active record so just use normal mysql to achieve your goal.


Query-string to Active Records - El Forum - 01-23-2013

[eluser]Patrick Reck[/eluser]
[quote author="Otemu" date="1358952905"]Hi,

You can't do sub queries in active record, there a some libraries that allow you to achieve this if you google for them. Your not forced to use active record so just use normal mysql to achieve your goal.[/quote]

I was just hoping for a Active Record solution. I guess I'll just have to do it the old way, thank you