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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:56:38+00:00 2026-06-18T09:56:38+00:00

I’m developing messaging system and have a trouble writing down the query(MYSQL). In the

  • 0

I’m developing messaging system and have a trouble writing down the query(MYSQL).
In the nutshell query should return an array of one last messages for each conversation user to user.

CREATE TABLE 'tbl_message'('id' int(10) AUTO_INCREMENT,
  'from_user' int(10),
  'to_user' int(10),
  'reply_to' int(10),
  'subject',
  'body' text,
  'status' tinyint(3),
  'deleted_from_sender' tinyint(3),
  'deleted_from_recipient' tinyint(3)',
  'created_on' datetime,

I wrote query:

SELECT * FROM tbl_message s1 WHERE s1.created_on IN (
   SELECT MAX(s2.created_on)
      FROM tbl_message s2 WHERE s1.from_user=".$userID." OR s2.to_user=".$userID."
      GROUP BY s2.from_user, s2.to_user) 
       AND status = 0";

That works fine but give me 2 last messages from from_user and to_user, instead of 1 last message from both. That because of GROUP BY of course, now the question is how I could find the max created_on(actually mysql datestamp) in subquery? Or any other solution will be appreciated.
Appreciate any help or advice.

After 2 days digging stackoverflow and mysql manuals hope for help from DB prof:)

UPD:
some data for example

+----+-------+--------+--------+---------------------+--------+---------+
| id | from_user | to_user | subject      | created_on          | status |
+----+-------+--------+--------+---------------------+--------+---------+
|  1 |   68      |   128   | somesubject1 | 2013-07-01 21:31:29 |      0 |
+----+-------+--------+--------+---------------------+--------+---------+
|  2 |    128    |   68    | somesubject2 | 2013-07-01 21:41:29 |      0 |
+----+-------+--------+--------+---------------------+--------+---------+
|  3 |    128    |   68    | somesubject3 | 2013-07-01 21:51:29 |      0 |
+----+-------+--------+--------+---------------------+--------+---------+
|  4 |    68     |   226   | somesubject4 | 2013-07-01 22:01:29 |      0 |
+----+-------+--------+--------+---------------------+--------+---------+

output of query

|  3 |    128    |   68    | somesubject3 | 2013-07-01 21:51:29 |      0 |
+----+-------+--------+--------+---------------------+--------+---------+
|  4 |    68     |   226   | somesubject4 | 2013-07-01 22:01:29 |      0 |
  • 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-18T09:56:39+00:00Added an answer on June 18, 2026 at 9:56 am

    You could use this query to get the maximum created_on date between two users:

    SELECT
      LEAST(from_user, to_user) user1,
      GREATEST(from_user, to_user) user2,
      MAX(created_on) max_created_on
    FROM
      tbl_message
    GROUP BY
      LEAST(from_user, to_user),
      GREATEST(from_user, to_user)
    

    and the query you are looking for is probably this:

    SELECT t.*
    FROM
      tbl_message t INNER JOIN (
        SELECT
          LEAST(from_user, to_user) user1,
          GREATEST(from_user, to_user) user2,
          MAX(created_on) max_created_on
        FROM
          tbl_message
        WHERE from_user=68 or to_user=68
        GROUP BY
          LEAST(from_user, to_user),
          GREATEST(from_user, to_user)) M
      on t.created_on = M.max_created_on
         AND LEAST(t.from_user, t.to_user)=user1
         AND GREATEST(t.from_user, t.to_user)=user2
    

    See fiddle with both queries here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have an array which has BIG numbers and small numbers in it. I
I've tracked down a weird MySQL problem to the two different ways I was
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I am confused How to use looping for Json response Array in another Array.
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.