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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:51:16+00:00 2026-06-07T02:51:16+00:00

I want to implement a message system using mysql for storage. This is the

  • 0

I want to implement a message system using mysql for storage.

This is the table:

CREATE TABLE `message` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `id_from` int(10) unsigned NOT NULL,
  `id_to` int(10) unsigned NOT NULL,
  `time` datetime NOT NULL,
  `message` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
);

id_from is the msg sender, id_to is the msg receiver .

Below inserting 4 messages for testing purposes:

INSERT INTO `message`(`id`,`id_from`,`id_to`,`time`,`message`) VALUES
(1,1,2,'2012-07-05 12:18:49','msg1'),
(2,2,1,'2012-07-05 12:18:58','msg2'),
(3,3,1,'2012-07-05 12:19:04','msg3'),
(4,1,3,'2012-07-05 12:19:10','msg4');

What I want to do and not succeeding, is building a query that fetches the last sent or received message for each pair (id_from, id_to) for a specific user.
In this case, the result would be:

row1: 2,2,1,'2012-07-05 12:18:58','msg2'
row2: 4,1,3,'2012-07-05 12:19:10','msg4'
  • 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-07T02:51:18+00:00Added an answer on June 7, 2026 at 2:51 am
    SELECT id, id_from, id_to, time, message
    FROM (  SELECT 
            FROM message
            WHERE 1 IN (id_from, id_to) # 1 is user_id in this case
            ORDER BY time DESC) AS h
    GROUP BY LEAST(id_from, id_to), GREATEST(id_from, id_to)
    

    Solution for gaining the last sent and received message (not what’s asked for though)

    SELECT id, id_from, id_to, time, message
    FROM (  SELECT 
            FROM message
            WHERE 1 IN (id_from, id_to) # 1 is user_id in this case
            ORDER BY time DESC) AS h
    GROUP BY id_from, id_to
    

    Should do it I guess.

    I’m sure there are “prettier” format to capture this in.

    Consider:

    SELECT id, 'from' AS type, id_from AS type_id, time, message
    FROM message
    WHERE id_to = 1
    ORDER BY time DESC
    LIMIT 1
    UNION ALL
    SELECT id, 'to', id_to, time, message
    FROM message
    WHERE id_from = 1
    ORDER BY time DESC
    LIMIT 1
    

    This would give you:

    id  type  type_id  time                 message
    3   from  3        2012-07-05 12:19:04  msg3
    4   to    3        2012-07-05 12:19:10  msg4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement message notification Gmail style in VB.NET Desktop Application? It will
i starter in jqgrid, i want implement inline edit in jqgrid i have this
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I'm using Symfony 2 security system. When some user trying to login, I want
I'm using WCF to implement a web service. This web service requires a 'ping'
I want to implement a facebook like notification system in ASP.NET MVC 3 :
I have IMessageSender interface. using System.ComponentModel.Composition; public interface IMessageSender { void Send(string message); }
Hey, I'm trying to implement a message system as used in social networks. The
I need to implement inter-process communication system for processes (in machine-wide) in Win32 using
This is the Exception message thrown by Gen_server when its not started. (ankit@127.0.0.1)32> R11

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.