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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:24:47+00:00 2026-05-28T08:24:47+00:00

Essentially what I want to do is select the most recent message between one

  • 0

Essentially what I want to do is select the most recent message between one user anyone they either received or sent a message to. This means that the unique user id I need could be either in the m_to or m_from. So how can I filter a search to select a unique id from one of two fields, where the field is not the main user’s id? Am I making sense here?

SELECT * FROM messages
WHERE m_to = '$user_id' OR m_from = '$user_id'
ORDER BY date

So just to (try to) be clear, I need to make a query that will select the most recent message either received or sent from the main user for every user they’ve received or sent a message to. This way there aren’t duplicated users in the list. However since this could either be in the to or from field, I have no clue how to do this. Can anyone lend me a hand here?

EDIT: Okay, apparently I wasn’t clear. the $user_id variable is the user id of the logged in user. Meaning I need unique messages from either the m_from or m_to field which is NOT $user_id

EDIT:

If we’re running this query…

SELECT
the_user,
the_other_party,
MAX(date) AS max_date
FROM (
SELECT
CASE m_to WHEN ‘IxjXgC’ THEN m_to ELSE m_from END AS the_user,
CASE m_from WHEN ‘IxjXgC’ THEN m_to ELSE m_from END AS the_other_party,
date
FROM messages
WHERE m_to = ‘IxjXgC’
OR m_from = ‘IxjXgC’
) m
and let’s say we have these categories in the database…

m_to    m_from     date
IxjXgC  ShJiMr   2012-01-22 01:20:37
ShJiMr  IxjXgC   2012-01-22 02:22:37
LhJiM1  IxjXgC   2012-01-22 03:23:37

and I run the query above, then the result becomes…

the_other_party     date
ShJiMr              2012-01-22 03:23:37
  • 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-05-28T08:24:48+00:00Added an answer on May 28, 2026 at 8:24 am

    I will not accuse you of being lazy (I’m no better than you in that regard, I’m afraid), but I’ll agree with Andreas Rohde in that you should exert yourself to listing the necessary columns explicitly as often as possible. Doing so may really help you to see solutions that otherwise would be less obvious, if at all.

    Take your present problem, for instance. You need a sort of GROUP BY query that would disregard a user’s status as a sender/receiver. Why, you could simply substitute sender for receiver and vice versa, in once case, and leave them unchanged in the other. Consider this then:

    SELECT
      CASE m_to   WHEN '$user_id' THEN m_to ELSE m_from END AS the_user,
      CASE m_from WHEN '$user_id' THEN m_to ELSE m_from END AS the_other_party,
      date
    FROM messages
    WHERE m_to   = '$user_id'
       OR m_from = '$user_id'
    

    The result set will contain rows where the user in question will always be returned as the_user, and the other party, well, as the_other_party.

    Of course, since we made sure that '$user' is returned in the same column, the first CASE can be got rid of:

    SELECT
      '$user_id' AS the_user,
      CASE m_from WHEN '$user_id' THEN m_to ELSE m_from END AS the_other_party,
      date
    FROM messages
    WHERE m_to   = '$user_id'
       OR m_from = '$user_id'
    

    That is, if you need that column at all.

    Anyway, you can now aggregate the result set of the above query, e.g. like this:

    SELECT
      the_user,
      the_other_party,
      MAX(date) AS max_date
    FROM (
      SELECT
        CASE m_to   WHEN '$user_id' THEN m_to ELSE m_from END AS the_user,
        CASE m_from WHEN '$user_id' THEN m_to ELSE m_from END AS the_other_party,
        date
      FROM messages
      WHERE m_to   = '$user_id'
         OR m_from = '$user_id'
    ) m
    GROUP BY
      the_user,
      the_other_party
    

    You can also join the last query as a subselect back to messages on (messages.m_to = agg.the_user OR messages.m_from = agg.the_user) AND messages.date = agg.max_date to get the rest of the columns.

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

Sidebar

Related Questions

I have one table with 2 columns that i essentially want to split into
I essentially want to spider my local site and create a list of all
Essentially I want to know if in VB.NET 2005 if using a sqlcommand and
We have a reverse proxy here, running Apache in version 2.2.x Essentially I want
Essentially what I want to do is to apply additional CSS classes to individual
If I want to essentially grep every line ever in the repository, is there
I want to write a piece of software which is essentially a regex data
Essentially, what I want to do (in DirectX) is to take two partially-transparent images
I want to store a list of numbers (essentially, a set in mathematical terms)
I want to implement a view in an iPhone application that is essentially like

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.