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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:04:25+00:00 2026-06-09T18:04:25+00:00

I have a table structure where I keep records of the messages sent and

  • 0

I have a table structure where I keep records of the messages sent and received like below;

Database table structure

member_id1 is the sender and member_id2 is the receiver.

Now what I’m trying to achieve is to get the rows with the most recent messages of the user who has the member_id of 3. The result should contain only 2 rows with the message ids of 2 and 4, as 2 is the last message between the user with the member_id of 3 and the user with the member_id of 2, and 4 is the only message between the user with the member_id of 3 and the user with the member_id of 1 yet.

The result shouldn’t contain the rows with the message_id of 3 for it has nothing to do with the user who has the member_id of 3, and the message_id of 1 because it has been overdated by the message with the message_id of 2.

Hope I’ve made my question clear for people who can help me with this problem.

  • 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-09T18:04:26+00:00Added an answer on June 9, 2026 at 6:04 pm

    I think I’ve figured out the right answer for my own question. Although I’m not quite sure, it seems to work great. For those who need a solution to such a problem; give this a try:

    SELECT m1.* 
      FROM table_name m1 
      INNER JOIN (SELECT MAX(senddate) AS senddate, 
                         IF(member_id2 = 3, member_id1, member_id2 ) AS user 
                    FROM table_name 
                   WHERE (member_id1 = 3 AND delete1=0) OR 
                         (member_id2 = 3 AND delete2=0) 
                  GROUP BY user) m2 
             ON m1.senddate = m2.senddate AND 
               (m1.member_id1 = m2.user OR m1.member_id2 = m2.user) 
         WHERE (member_id1 = 3 AND delete1=0) OR 
               (member_id2 = 3 AND delete2=0) 
       ORDER BY m1.senddate DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an table structure like this mysql> SELECT id, name, parent_id FROM categories;
I have a table structure having a EMP_DATE column as below ID EMP_DATE ----
I have a table structure with columns like this [ID] [Name] [ParentId] [ParentName] The
I have this table structure and would like to map it using Fluent Hibernate
I have a table structure like this: <table> <tr> <td> <ul> <li class=check></li> </ul>
I have my Table structure like this :: ATT_Table : Fields - Act_ID, Assigned_To_ID,
I have a table (a single table) like the picture below. to find the
i have table structure with 3 colums (column1, column2, column3) and i want to
I have table structure: ous.txt 1452 1793 out.txt 36796 14997 ouw.txt 478 4247 3
I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE

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.