Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8268959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:04:05+00:00 2026-06-08T06:04:05+00:00

Codeigniter; Active Records Class From that topic, I asked a question regarding CodeIgniter, I’ve

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:04:06+00:00Added an answer on June 8, 2026 at 6:04 am

    Here is a solution. I don’t think it is the most efficient solution but it should work. Note that I moved the Topics SELECT to the first SELECT row.

    SELECT `forums`.*, 
            Count(topics.id) threads, 
            Count(replies.id) replies, 
            `users`.`url` user_url, 
            `users`.`name` user_name,
            `ranks`.`name` user_rank,
            (SELECT `topics`.`url` 
             FROM `topics` 
             WHERE `topics`.`f_id` = `forums`.`id` 
             ORDER BY `created` DESC LIMIT 1) topic_url,
            (SELECT  `topics`.`name`
             FROM `topics` 
             WHERE `topics`.`f_id` = `forums`.`id` 
             ORDER BY `created` DESC LIMIT 1) topic_name 
     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`  
     GROUP BY `forums`.`id`
    

    Possible more efficient way

    I don’t have the data to test whether this is faster but it is another way of doing it (I based this off of ROW_NUMBER() in MySQL.) This comes with one string attached and that is that the topics table can’t have duplicate f_id,created combinations:

    SELECT `forums`.*, 
            Count(topics.id) threads, 
            Count(replies.id) replies, 
            `users`.`url` user_url, 
            `users`.`name` user_name,
            `ranks`.`name` user_rank,
            t.url topic_url,
            t.name topic_name
     FROM (`forums`)  
     LEFT JOIN (SELECT t0.id, t0.f_id, t0.url, t0.name, t0.a_id
                FROM topics AS t0
                LEFT JOIN topics AS t1 ON t0.f_id=t1.f_id AND t1.created>t0.created
                WHERE t1.id IS NULL) AS t
                ON t.f_id = forums.id
     LEFT JOIN `replies` ON `replies`.`t_id` = t.`id`  
     LEFT JOIN `users` ON `users`.`id` = t.`a_id`  
     LEFT JOIN `ranks` ON `users`.`status` = `ranks`.`id` 
     GROUP BY `forums`.`id`
    

    Try it out, first to see if it works, and then to see if it is faster. I’d love to hear back from you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using multiple databases using CodeIgniter Active Records class. I had to disable
I'm using CodeIgniter's Active Record class to query the MySQL database. I need to
I'm using Codeigniter and the Active Record class to build a simple API in
Is it possible to write this query in codeigniter active records? $Main_Nav_Query = mysql_query(SELECT
I'm tinkering with CodeIgniter and have come across Active Records for the first time.
I want to implement a sql query using CodeIgniter Active Record class. The query
In which function/class is the dbprefix added to the table name in Codeigniter Active
Using CodeIgniter's Active Record class and MySQL, I have a table of posts with
My question is about inserting data with CodeIgniter Active record. There's an example on
I'm using Codeigniter's Active Record Class. So the query looks something like this: $query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.