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

The Archive Base Latest Questions

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

I am creating a small message board and I am stuck I can select

  • 0

I am creating a small message board and I am stuck

I can select the subject, the original author, the number of replies but what I can’t do is get the username, topic or date of the last post.

There are 3 tables, boards, topics and messages.

I want to get the author, date and topic of the last message in the message table. The author and date field are already fields on the messages table but i would need to join the messages and topics table on the topicid field.

this is my query that selects the subject, author, and number of replies

SELECT t.topicname, t.author, count( message ) AS message   FROM topics t   INNER JOIN messages m     ON m.topicid = t.topicid   INNER JOIN boards b     ON b.boardid = t.boardid   WHERE b.boardid = 1   GROUP BY t.topicname 

Can anyone please help me get this finished?

This is what my tables look like

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` timestamp(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`) ); 
  • 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-11T14:28:20+00:00Added an answer on May 11, 2026 at 2:28 pm

    if your SQL supports the LIMIT clause,

    SELECT m.author, m.date, t.topicname FROM messages m  JOIN topics t ON m.topicid = t.topicid  ORDER BY date desc LIMIT 1 

    otherwise:

    SELECT m.author, m.date, t.topicname FROM messages m  JOIN topics t ON m.topicid = t.topicid  WHERE m.date = (SELECT max(m2.date) from messages m2) 

    EDIT: if you want to combine this with the original query, it has to be rewritten using subqueries to extract the message count and the date of last message:

    SELECT t.topicname, t.author,        (select count(message) from messages m where m.topicid = t.topicid) AS messagecount,         lm.author, 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   WHERE b.boardid = 1   GROUP BY t.topicname 

    also notice that if you don’t pick any field from table boards, you don’t need the last join:

    SELECT t.topicname, t.author,        (select count(message) from messages m where m.topicid = t.topicid) AS messagecount,         lm.author, lm.date   FROM topics t   INNER JOIN messages lm     ON lm.topicid = t.topicid AND lm.date = (SELECT max(m2.date) from messages m2)   WHERE t.boardid = 1   GROUP BY t.topicname 

    EDIT: if mysql doesn’t support subqueries in the field list, you can try this:

    SELECT t.topicname, t.author, mc.messagecount, lm.author, lm.date   FROM topics t   JOIN (select m.topicid, count(*) as messagecount from messages m group by m.topicid) as mc      ON mc.topicid = t.topicid   JOIN messages lm     ON lm.topicid = t.topicid AND lm.date = (SELECT max(m2.date) from messages m2)   WHERE t.boardid = 1   GROUP BY t.topicname 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im creating a simple message board with php/mysql. Users enter their name and a
I'm creating a small server using java.nio , but when trying to stress test
I am creating a small modal form that is used in Winforms application. It
I am creating a small application that will be deployed on Window. The database
I am creating a small app to teach myself ASP.NET MVC and JQuery, and
I'm creating a small app in ASP.NET MVC that generates ics (iCal) files based
I am creating a small silverlight widget which pronounces a word and highlights each
I'm creating a small code plugin which allows you to do some things with
I am creating a small console app that needs a progress bar. Something like...
I'm creating a small application on an embedded device that has a boa web

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.