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 165831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:59:28+00:00 2026-05-11T11:59:28+00:00

This is a follow up on another problem i had with getting-the-last-record-inserted-into-a-select-query I am

  • 0

This is a follow up on another problem i had with getting-the-last-record-inserted-into-a-select-query

I am trying to edit a query that Andrea was kind enough to help me with yesterday which works fine for one page but I am trying to create a similar query without much luck.

What I need to to is for every board display the board name, the count of topics and messages linked to that board and the user, topic and date of the last message (which does work)

What i need is to get the board name, the topic and message count

This is my table structure

CREATE TABLE `boards` (   `boardid` int(2) NOT NULL auto_increment,   `boardname` varchar(255) NOT NULL default '',   PRIMARY KEY  (`boardid`) );  CREATE TABLE `messages` (   `messageid` int(6) NOT NULL auto_increment,   `topicid` int(4) NOT NULL default '0',   `message` text NOT NULL,   `author` varchar(255) NOT NULL default '',   `date` datetime(14) NOT NULL,   PRIMARY KEY  (`messageid`) );  CREATE TABLE `topics` (   `topicid` int(4) NOT NULL auto_increment,   `boardid` int(2) NOT NULL default '0',   `topicname` varchar(255) NOT NULL default '',   `author` varchar(255) NOT NULL default '',   PRIMARY KEY  (`topicid`) ); 

and the query I have come up with based on the query that Andrea did for me. What this query outputs in the boardname, the number of topics and messages (which says 1 even though there are 5), the topic author and messagecount (which isn’t needed), the author and date of the last post (which is needed) but not the topic name which is needed

SELECT b.boardname, count( DISTINCT t.topicname ) AS topics, count( lm.message ) AS message, t.author as tauthor,         (select count(message) from messages m where m.topicid = t.topicid) AS messagecount,         lm.author as lauthor, lm.date   FROM topics t   INNER JOIN messages lm     ON lm.topicid = t.topicid AND lm.date = (SELECT max(m2.date) from messages m2)   INNER JOIN boards b     ON b.boardid = t.boardid   GROUP BY t.topicname 

This my original query that does what I wanted but get the first post, not the last

SELECT b.boardid, b.boardname, count( DISTINCT t.topicname ) AS topics, count( m.message ) AS message, m.author AS author, m.date AS date, t.topicname AS topic FROM boards b INNER JOIN topics t ON t.boardid = b.boardid INNER JOIN messages m ON t.topicid = m.topicid INNER JOIN (  SELECT topicid, MAX( date ) AS maxdate FROM messages GROUP BY topicid ) test ON test.topicid = t.topicid GROUP BY boardname ORDER BY boardname 

any help with this much appreciated

  • 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. 2026-05-11T11:59:29+00:00Added an answer on May 11, 2026 at 11:59 am
    SELECT  b.*, m.*, t,*         (         SELECT  COUNT(*)         FROM    topics ti         WHERE   ti.boardid = b.boardid         ) AS topiccount,         (         SELECT  COUNT(*)         FROM    topics ti, messages mi         WHERE   ti.boardid = b.boardid                 AND mi.topicid = ti.topicid         ) AS messagecount FROM    boards b LEFT JOIN         messages m ON      m.messageid = (         SELECT  mii.messageid         FROM    topics tii, messages mii         WHERE   tii.boardid = b.boardid                 AND mii.topicid = tii.topicid         ORDER BY                 mii.date DESC         LIMIT 1         ) LEFT JOIN         topics t ON      t.topicid = m.topicid 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.