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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:30:41+00:00 2026-06-10T12:30:41+00:00

Lets say this is the database structure: SELECT * FROM `pms` where id_to =

  • 0

Lets say this is the database structure:

enter image description here

SELECT * FROM `pms` where id_to = 1 or id_from = 1

This would return all the messages that he has recived or sent,

So how can I retrieve the last message from each conversation that the user 1 may have?

PD: I call it conversation when there is one or more messages between two users

-edit-

So given this database content:

enter image description here

We want to get id 4 and 6

  • 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-10T12:30:43+00:00Added an answer on June 10, 2026 at 12:30 pm

    This assumes id is an auto-increment column:

    SELECT MAX(id) AS id
    FROM pms
    WHERE id_to = 1 OR id_from = 1
    GROUP BY (IF(id_to = 1, id_from, id_to))
    

    Assuming you have id_from and id_to indexed, this variation will most likely perform better because MySQL doesn’t know what to do with an OR:

    SELECT MAX(id) AS id FROM
    (SELECT id, id_from AS id_with
    FROM pms
    WHERE id_to = 1
    UNION ALL
    SELECT id, id_to AS id_with
    FROM pms
    WHERE id_from = 1) t
    GROUP BY id_with
    

    Here’s how to get the messages for those ids:

    SELECT * FROM pms WHERE id IN
        (SELECT MAX(id) AS id FROM
        (SELECT id, id_from AS id_with
        FROM pms
        WHERE id_to = 1
        UNION ALL
        SELECT id, id_to AS id_with
        FROM pms
        WHERE id_from = 1) t
        GROUP BY id_with)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that I have a database structure with three tables that look like
I have multiple tables in a MySQL database. Lets say they look like this:
I have a MySQL database I'm searching through. Lets say this is a database
Let's say i have a database design like this: for the data: id |
I was wondering how exactly does TCP implement in-order delivery. lets say this is
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have this code: <?php class hello { var $greeting = hello;
Lets say i have this usercontrol public class test : UserControl { public int
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }
Lets say we have this code: bool KeepGoing = true; DataInThread = new Thread(new

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.