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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:17:51+00:00 2026-06-14T08:17:51+00:00

I am trying to get some fields from the following database: CREATE TABLE `messages`

  • 0

I am trying to get some fields from the following database:

CREATE TABLE `messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message` text,
  `date` datetime DEFAULT NULL,
  `fra` int(11) DEFAULT NULL,
  `til` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;

Fra = sender ID;
til = Receiver ID

That has for example this values:

(1, 'This is a message for Obama', '2012-11-14 01:12:06', 1, 3),
(2, 'Hello John! How are you?', '2012-11-14 01:23:43', 3, 1),
(3, 'Hey Jack', '2012-11-14 01:28:11', 1, 2),
(4, 'How are you son?', '2012-11-14 01:28:15', 1, 2),
(5, 'everything allright for you?', '2012-11-14 01:28:23', 1, 2);

The way I want to get the fields from the database is at follows:
I want to get the unique conversations from one user to another. If user 1 has sent a message to user 3, and user 3 replied to user 1, then I would like only to select the last field into the database; Or if user 1 has sent 3 messages to user 2, then I would like to get only the 3rd message to user 2, from user 1.
So that means I should only get 2 rows as result

I have tried:

SELECT DISTINCT * FROM messages WHERE fra='".$_SESSION['userid']."' OR til='".$_SESSION['userid']."' GROUP BY fra,til"

and

"SELECT DISTINCT(til) AS til FROM messages WHERE fra='".$_SESSION['userid']."' OR til='".$_SESSION['userid']."'  ORDER BY id DESC"

but I can’t get the result I want.

Can someone explain me how to do this?

Edit: I want only to build threads of user messages that will show the last message sent between two users (that’s the scope of the query)

  • 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-14T08:17:52+00:00Added an answer on June 14, 2026 at 8:17 am

    Here’s the answer/example for the sql statement: http://sqlfiddle.com/#!2/ae56b/10

    select * 
    from messages m1
    where m1.date = (select max(m2.date) 
        from messages m2 
        where 
        case when m2.fra < m2.til then m2.fra else m2.til end = 
        case when m1.fra < m1.til then m1.fra else m1.til end
        and 
        case when m2.fra > m2.til then m2.fra else m2.til end = 
        case when m1.fra > m1.til then m1.fra else m1.til end
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get some data out from a database stored as text
I'm having a 'mare trying to get some simple data from my MySQL database.
Just trying to get some rows out of a database and loop through but
I'm trying to get some data from a certain web-based api using the libcurl
I am trying to return a result set from a MySQL database table of
I am trying to make some data from database available using XML, I have
I'm trying to create a registration form. In this form there are some fields(username,password
I'm trying to get data from one database and put it in the format
I am trying to obtain a float value from my Database The following checks
Im trying to get some auto complete to work. I have this URL i

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.