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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:27:29+00:00 2026-06-15T13:27:29+00:00

I have two mysql tables Users & Messages. Please will you help me to

  • 0

I have two mysql tables Users & Messages. Please will you help me to create an sql query which returns only the most recent message either sent or received by the logged in user Mike ( user_id=1 ) along with the message_id, message_text & first_name of the person who the message was sent to or received from. Ordered by message_id in descending order.

So far i have the query:-

SELECT message_id, first_name, message_text FROM tbl_users, tbl_messages 
WHERE 
(tbl_users.user_id = tbl_messages.sender_id AND tbl_messages.receiver_id = 1 
OR tbl_users.user_id = tbl_messages.receiver_id AND tbl_messages.sender_id = 1) 
GROUP BY tbl_users.first_name
ORDER BY message_id DESC

Which gives the result in the image below. It seems the ORDER BY is being ignored

Group by result

Thank you in advance

tbl_users

---------------------------
| user_id |  first_name   |
---------------------------
| 1       |  Mike         |
| 2       |  John         |
| 3       |  George       |
| 4       |  Peter        |
| 5       |  Sarah        |
---------------------------

tbl_messages

----------------------------------------------------------------
| message_id |  sender_id   |  receiver_id   |  message_text   |
----------------------------------------------------------------
| 1          |  2           | 1              |  Hello          |
| 2          |  3           | 1              |  How are you    |
| 3          |  1           | 5              |  Hi there       |
| 4          |  2           | 1              |  Greetings      |
| 5          |  1           | 4              |  Good day       |
| 6          |  3           | 1              |  Hi             |
| 7          |  5           | 1              |  A message      |
| 8          |  5           | 4              |  Good morning   |
| 9          |  1           | 5              |  Hello dear     |
| 10         |  1           | 3              |  Howdy          |
----------------------------------------------------------------

Desired result

----------------------------------------------
| message_id |  first_name  |  message_text  |
----------------------------------------------
| 10         |  George      | Howdy          |
| 9          |  Sarah       | Hello dear     |
| 5          |  Peter       | Good day       |
| 4          |  John        | Greetings      |
----------------------------------------------

Database is available here

  • 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-15T13:27:30+00:00Added an answer on June 15, 2026 at 1:27 pm

    Just adding a GROUP BY clause may solve your problem, try this…

    SELECT message_id, first_name, message_text FROM tbl_users, tbl_messages 
    WHERE  
    (tbl_users.user_id = tbl_messages.sender_id AND tbl_messages.receiver_id = 1 
    OR tbl_users.user_id = tbl_messages.receiver_id AND tbl_messages.sender_id = 1) 
    GROUP BY tbl_users.user_id
    ORDER BY message_id DESC
    

    UPDATE: Changing the whole query

    SELECT t2.message_id, t2.first_name, t2.message_text FROM
    (SELECT u.user_id, m.message_id, u.first_name, m.message_text FROM `tbl_users` u
      JOIN `tbl_messages` m ON (u.user_id = m.sender_id AND m.receiver_id = 1)
        OR (u.user_id = m.receiver_id AND m.sender_id = 1) 
      ORDER BY m.message_id DESC
    ) t2
    GROUP BY t2.user_id
    ORDER BY t2.message_id DESC
    

    RESULT:

    +------------+------------+--------------+
    | message_id | first_name | message_text |
    +------------+------------+--------------+
    | 10         | George     | Howdy        |
    | 9          | Sarah      | Hello dear   |
    | 5          | Peter      | Good Day     |
    | 4          | John       | Greetings    |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this mysql query (evolving two tables : Users and Files ) that's
I have two mysql tables: /* Table users */ CREATE TABLE IF NOT EXISTS
using php and mysql I have two tables, a users table and a profiles
I have two MySQL tables: stats (left) and messages (right) +------------+---------+ +---------+------------+-----------+----------+ | _date
I have two tables (claim_pics & claiminfo) I am doing a query for a
I have two tables users and items in a MySQL DB Users pk username
I have two tables on mysql: users, and management. The users table has a
Here's the situation. I have two tables: users (registered users of the website), messages
Hi I have two tables (users & friends) I need to connect to the
Ok, I have two MYSQL tables: CREATE TABLE `sessions` ( `id` int(11) NOT NULL

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.