CodeIgniter Forums

Full Version: Query-string to Active Records
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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'

El Forum

[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.

El Forum

[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