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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:41:43+00:00 2026-05-15T12:41:43+00:00

I am having trouble with a SQL query. SELECT t.topicname, m. *, ms.avatar FROM

  • 0

I am having trouble with a SQL query.

SELECT t.topicname, m. *, ms.avatar
   FROM `messages` m
    INNER JOIN topics t ON m.topicid = t.topicid
    inner join users u on m.author=u.username
    inner join misc ms on u.userid=ms.userid
   ORDER BY postdate DESC LIMIT 5

what i want to do is get the topicname from the topics table, everything from the messages table and avatar from the misc table

I join the topics and messages table by the topicid
I can join the messgages and users table by messages.author and users.username and to join to the misc table i join on users.userid and misc.userid

The problem is I am not getting the results I want

This query works on the messages and topics tables joined but i need to add the misc table and the only way to join that is throught the users table

SELECT t.topicname, m. *
   FROM `messages` m
    INNER JOIN topics t ON m.topicid = t.topicid
    ORDER BY postdate DESC LIMIT 5

Here is the table structure

CREATE TABLE `messages` (
  `messageid` int(6) NOT NULL auto_increment,
  `boardid` int(2) NOT NULL default '0',
  `topicid` int(4) NOT NULL default '0',
  `message` text NOT NULL,
  `author` varchar(255) NOT NULL default '',
  `postdate` datetime default NULL,
  PRIMARY KEY  (`messageid`)
)

CREATE TABLE `misc` (
  `miscid` int(4) NOT NULL auto_increment,
  `userid` int(4) NOT NULL default '0',
  `profpic` varchar(100) NOT NULL default '',
  `avatar` varchar(100) NOT NULL default '',
  `signature` text NOT NULL,
  `alerts` enum('y','n') NOT NULL default 'y',
  PRIMARY KEY  (`miscid`)
)

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 '',
  `counter` int(5) NOT NULL default '0',
  `sticky` char(1) NOT NULL default 'n',
  `locked` char(1) NOT NULL default 'n',
  PRIMARY KEY  (`topicid`)
)

CREATE TABLE `users` (
  `userid` int(25) NOT NULL auto_increment,
  `first_name` varchar(25) NOT NULL default '',
  `last_name` varchar(25) NOT NULL default '',
  `email` varchar(255) NOT NULL default '',
  `username` varchar(25) NOT NULL default '',
  PRIMARY KEY  (`userid`)
)

This is the output of the query with only 2 joins and this is what i expect to get (only i want to have an avatar with it)

It doesn’t llok to pretty here but that is what the database gives me

topicname         messageid       boardid         topicid         message         author      postdate
Mauris Eu Neque Ipsum   36  4   8   Suspendisse nibh risus, porta at cursus sed, tinci...   iTuneStinker    2010-04-08 20:31:39
Aliquam Erat Volutpat   35  5   15  Donec volutpat ligula eu lorem pharetra a adipisci...   AwsomeMoon  2010-04-07 21:58:20
Ut Non Risus Elit   34  2   14  Etiam cursus, erat sed placerat fringilla, risus a...   ScaryHair   2010-04-07 15:35:34
Quisque Rutrum Mattis Sagittis  33  5   9   Etiam in elit sit amet nulla scelerisque ultricies...   ScaryHair   2010-04-07 15:33:46
Where Do I Start Trying To Organise A Festival  32  3   12  Morbi a neque aliquam nisl varius lobortis. Sed ve...   ScaryHair   2010-04-07 13:27:40

and here is the result using the query that joins all tables

topicname         messageid       boardid         topicid         message         author      postdate        avatar
Forum Rules And Guidelines  2   1   1   FORUM RULES     AdRock  2009-04-11 23:05:18     avatar_17200.jpg
Forum Rules And Guidelines  3   2   2   FORUM RULES.    AdRock  2009-04-11 23:05:18     avatar_17200.jpg
Forum Rules And Guidelines  4   3   3   FORUM RULES     AdRock  2009-04-11 23:05:18     avatar_17200.jpg
Forum Rules And Guidelines  5   4   4   FORUM RULES     AdRock  2009-04-11 23:05:18     avatar_17200.jpg
Forum Rules And Guidelines  6   5   5   FORUM RULES     AdRock  2009-04-11 23:05:18     avatar_17200.jpg

The first quert gets the last 5 records of the messages table and that is what i want but i want to be able to get the avatar that relates to the author of each message

  • 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-05-15T12:41:44+00:00Added an answer on May 15, 2026 at 12:41 pm

    Seems you don’t have users and avatars filled for each message.

    Try replacing the INNER JOINS with the OUTER JOINS.

    SELECT  t.topicname, m. *, ms.avatar
    FROM    `messages` m
    JOIN    topics t
    ON      t.topicid = m.topicid
    LEFT JOIN
            users u
    ON      u.username = m.author
    LEFT JOIN
            misc ms
    ON      ms.userid = u.userid
    ORDER BY
            m.postdate DESC
    LIMIT 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 431k
  • Answers 431k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To combine all the suggestions into one: // canonicalize paths… May 15, 2026 at 2:10 pm
  • Editorial Team
    Editorial Team added an answer You have to setup the server to allow ssl connections.… May 15, 2026 at 2:10 pm
  • Editorial Team
    Editorial Team added an answer Try using: string path = Server.MapPath(typeModel.ArtUrl); MSDN Link May 15, 2026 at 2:10 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.