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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:24:37+00:00 2026-06-18T00:24:37+00:00

hello i have this SQL witch is supposed to give me the max(messages.message_id) so

  • 0

hello i have this SQL witch is supposed to give me the max(messages.message_id)
so i have the record where the first message_id is 7 and the last message_id is 10, but instead of giving me 10 it gives me 7… it is completely ignoring the MAX(messages.message_id) and giving me the first message_id… any suggestions on how to fix this?

SELECT
  profile.first_name,
  profile.last_name,
  conversations.conversation_hash,
  conversations.person_a,
  conversations.person_b,
  messages.conversation_hash,
  MAX(messages.message_id),
  messages.message,
  messages.subject,
  messages.date
FROM conversations
  INNER JOIN messages
    ON conversations.conversation_hash = messages.conversation_hash
  INNER JOIN profile
    ON profile.id = conversations.person_b
WHERE conversations.person_a = '$id'
GROUP BY messages.conversation_hash
ORDER BY messages.message_id DESC

tables:
conversations:

conversation_id | conversation_hash | person_a | person_b |

messages:

conversation_hash | from_id | to_id | message_id | subject | message | date
  • 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-18T00:24:38+00:00Added an answer on June 18, 2026 at 12:24 am

    You can separately get the latest message_id from table messages inside a subquery and the result of it is then join back against the tables provided that it match on two conditions: conversation_hash and message_id.

    Full Query:

    SELECT  profile.first_name,
            profile.last_name,
            conversations.conversation_hash,
            conversations.person_a,
            conversations.person_b,
            messages.*
    FROM    conversations
            INNER JOIN messages
                ON conversations.conversation_hash = messages.conversation_hash
            INNER JOIN  
            (
                SELECT  conversation_hash, MAX(message_id) max_ID
                FROM    messages
                GROUP   BY conversation_hash
            ) c ON messages.conversation_hash = c.conversation_hash AND
                    messages.message_id = c.max_ID
            INNER JOIN profile
                ON profile.id=conversations.person_b
    WHERE   conversations.person_a='$id'
    ORDER   BY messages.message_id DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everybody I have some certs in SQL 2008 but am by no means
Hello i have a SQL Table which look like this: ID BEZEICHNUNG PREIS UPDATE_DATE
Hello first post here, I have a doubt of how to implement this, i
hello i have this question i was trying to find a way to call
Hello I have this code class Triplets { public: int nVal1; int nVal2; NodeT
Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();
Hello I have this list that i am working on. When i move the
hello I have this code that use to sort a datatable. Dim sortingIndex As
Hello I have like this 2 tables class User public int UserId{get;set;} { ....
Hello I currently have this code checking if a cookie exists, it works all

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.