Codeigniter; Active Records Class
From that topic, I asked a question regarding CodeIgniter, I've thought of doing it completely raw now instead.
This is my current query.
SELECT `forums`.*,
Count(topics.id) threads,
Count(replies.id) replies,
`users`.`url` user_url,
`users`.`name` user_name,
`ranks`.`name` user_rank
FROM (`forums`)
LEFT JOIN `topics` ON `topics`.`f_id` = `forums`.`id`
LEFT JOIN `replies` ON `replies`.`t_id` = `topics`.`id`
LEFT JOIN `users` ON `users`.`id` = `topics`.`a_id`
LEFT JOIN `ranks` ON `users`.`status` = `ranks`.`id`
LEFT JOIN (
SELECT `topics`.`url` topic_url, `topics`.`name` topic_name
FROM `topics`
ORDER BY `created` DESC
LIMIT 1
) last_post ON `topics`.`f_id` = `forums`.`id`
GROUP BY `forums`.`id`
So what I am trying to do is to get everything in one nifty row. Everything beside "Last Post By" is working, if I remove the later LEFT JOIN
I added I get the first, and not the last post.
So my question is; What am I now doing wrong?
Also, a bonus question, I talked to a friend earlier that is a bit more knowledgable when it comes to PHP and MySQL than I am, he told me to just store it all in the database, last_post as well as the count of threads and replies.
Thanks in advance.
Update
Here is forums table
CREATE TABLE IF NOT EXISTS `forums` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c_id` int(11) NOT NULL,
`url` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`desc` text NOT NULL,
PRIMARY KEY (`id`),
KEY `url` (`url`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
Here is topics table
CREATE TABLE IF NOT EXISTS `topics` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`f_id` int(11) NOT NULL,
`url` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`desc` varchar(255) NOT NULL,
`body` text NOT NULL,
`a_id` int(11) NOT NULL,
`created` int(11) NOT NULL,
`edited` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `head` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
I also tried to update how it was displayed with the markdown editing, as I did the first time, I hope this is more appealing.