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

  • SEARCH
  • Home
  • 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 8269845
In Process

The Archive Base Latest Questions

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

I have started programming for sometime with PHP using MySQL as the database and

  • 0

I have started programming for sometime with PHP using MySQL as the database and I know basic SQL queries upto simple JOINS containing upto two tables.

But the moment I need to get results from 3 or more tables, I am stuck. No matter how hard I try, I still manage to find myself lost. I have searched everywhere looking for a good tutorial on how to tackle complex SQL queries but haven’t found anything that explains how to go about it. Most tutorials consists of solutions for a particular problem, but they don’t explain the perfect general procedure how to go about tackling a problem

Can anyone explain the basic general way of going about from start to finish, how to construct the query, etc when it comes to complex queries.

For example:

I have a forum with the following database structure:

forumCategory:

id | name | desc

forumTopic:

id | category_id | created_on | created_by | title | content

forumPost:

id | topic_id | created_on | created_by

users:

id | first_name | last_name

All topics are created in the forumTopic table. All replies to that topic are inserted in the forumPost table.

Now on the forum main page, I need to display the categories, the very last post posted by a user in that particular category, the user who posted the last post.

The flow I thought of was :

  1. Find the last post in a category by looking at the MAX(id) in the forumPost table grouped by topic_id. This will get me the ID of the last post in every topic.

  2. Now again find the MAX(of the IDS I got earlier) grouped by category_id. This will get me the last post in every category.

IDs are autoincrementing primary keys.

But I got stuck with constructing a SQL query from the above algorithm

Would be very helpful if someone could help me out on this one.

  • 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:21:59+00:00Added an answer on June 8, 2026 at 6:21 am

    Get the last post for each category by joining Post to Topic

    SELECT category_id , category.name, Max(ForumPost.ID) as maxpostid
    from ForumPost
    inner join ForumTopic on ForumPost.Topic_ID = ForumTopic.ID
    inner join ForumCategory on ForumTopic.Category_Id = ForumCategory.ID
    group by category_Id, category.name
    

    (This is an intermediate stage for explanatory purposes – it is included in the query below )

    Then join this to the user table to find out the user name (presumably a post has a userid?)

    select users.name, lastposts.*
    from
        forumpost
    inner join
        (
            SELECT category_id , category.name, Max(ForumPost.ID) as maxpostid
            from ForumPost
            inner join ForumTopic on ForumPost.Topic_ID = ForumTopic.ID
            inner join ForumCategory on ForumTopic.Category_Id = ForumCategory.ID
            group by category_Id, category.name
         ) lastposts
              on forumpost.id = lastposts.maxpostid
    inner join
         users on forumpost.userid =users.id
    

    However, you may want to consider updating the category table with the last post each time a post is made. That way you can run a much simpler query for your forum front page.

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

Sidebar

Related Questions

I have recently started programming in PHP. I am building a cart in PHP.
I have just started with Gui Programming in netbeans (Using the template Java Desktop
I recently started programming in WinForms using C#. I have a requirement where I
I have started a very simple project to learn iOS programming, but I get
i have been using PHP for quite a sometime now, having understood the concept
I have started using Vim for my C(++) programming. But I have 1 problem.
I have started to do some programming using VIM. I have very mixed feelings
I have started programming a few weeks ago in java/android. I want to write
I have recently started programming in WPF and bumped into the following problem. I
I have just started programming and have made a few small applications in C

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.