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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:07:06+00:00 2026-06-09T22:07:06+00:00

I am trying to implement a message system quite similar to facebook . The

  • 0

I am trying to implement a message system quite similar to facebook . The message table is :

+--------+----------+--------+-----+----------+
| msg_id | msg_from | msg_to | msg | msg_time |
+--------+----------+--------+-----+----------+

Here msg_from and msg_to contain user ids and the msg_time contains the timestamp of the message . A user’s user id can appear in both the to and from column and multiple times for another user . How should I write a SQL query which selects the most recent sent message between two users ? (The message can come from either one) 1 to 2 or 2 to 1 .

  • 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-09T22:07:08+00:00Added an answer on June 9, 2026 at 10:07 pm

    Since John Woo clarified that it is not directional, here’s my new answer:

    select *
    from msgsList
    where (least(msg_from, msg_to), greatest(msg_from, msg_to), msg_time)       
    in 
    (
        select 
           least(msg_from, msg_to) as x, greatest(msg_from, msg_to) as y, 
           max(msg_time) as msg_time
        from msgsList 
        group by x, y
    );
    

    Output:

    | MSG_ID | MSG_FROM | MSG_TO |    MSG |                       MSG_TIME |
    ------------------------------------------------------------------------
    |      1 |        1 |      2 |  hello | January, 23 2010 17:00:00-0800 |
    |      5 |        1 |      3 | me too | January, 23 2012 00:15:00-0800 |
    |      6 |        3 |      2 |  hello | January, 23 2012 01:12:12-0800 |
    

    For this input:

    create table msgsList
    (
      msg_id int,
      msg_from int, 
      msg_to int,
      msg varchar(10),
      msg_time datetime
    );
    
    insert into msgslist VALUES
    
    (1, 1, 2, 'hello', '2010-01-23 17:00:00'),      -- shown
    (2, 2, 1, 'world', '2010-01-23 16:00:00'),
    
    (3, 3, 1, 'i am alive', '2011-01-23 00:00:00'),
    (4, 3, 1, 'really', '2011-01-22 23:15:00'),
    (5, 1, 3, 'me too', '2012-01-23 00:15:00'),     -- shown
    
    (6, 3, 2, 'hello', '2012-01-23 01:12:12');      -- shown
    

    SQLFiddle Demo


    If ANSI SQL is your cup of tea, here’s the way to do it: http://sqlfiddle.com/#!2/0a575/19

    select *
    from msgsList z
    where exists
    (
        select null
        from msgsList
        where 
          least(z.msg_from, z.msg_to) = least(msg_from, msg_to)
          and greatest(z.msg_from, z.msg_to) = greatest(msg_from, msg_to)
        group by least(msg_from, msg_to), greatest(msg_from, msg_to)
        having max(msg_time) = z.msg_time  
    ) ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to implement a search similar to here .This searches properties based on city,locality,property
Hey, I'm trying to implement a message system as used in social networks. The
I'm trying to implement a private message system. Let me know if this is
I'm trying to implement this accepted solution for displaying a custom error message: https://stackoverflow.com/a/5229581/141172
Trying to implement a rating system of users and postings. What is the best
Trying to implement a UITableView of names similar to the built-in Contacts iPhone app
I've been trying to implement the Facebook SDK into my application in order to
Trying to implement a simple notification system based on private pub ( something like
I'm trying to implement (as part of a larger system) a feature where a
I'm trying to implement a simple messaging system in CakePHP 1.3. What I need

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.