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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:11:49+00:00 2026-05-12T15:11:49+00:00

I have a mysql table containing messages: id (INT) sender_id (INT) recipient_id (INT) sent_time

  • 0

I have a mysql table containing messages:

id (INT)
sender_id (INT)
recipient_id (INT)
sent_time (INT)
read_time (INT)
body (TEXT)

I need to retrieve a list of most recent messages user X received from or sent to other (distinct) users.

In other words I’d like to create a full list of conversations with other users, sorted by sent_time of last message exchanged with a given user.
The end result (processed in PHP) is going to like like this:

array (
    array (other_user_1_id, last_msg_sent_time, last_msg_is_read,
           last_msg_is_to_me/from_me, number_of_messages*),
    array (other_user_2_id, ... )
)

*) number_of_messages is optional, but rather nice to have.

Question: How do I do this most efficiently? Create a separate table “conversations” and update it every time anyone sends or reads a message or create a single, but sophisticated query on “messages” table? If the latter, what would the query look like?

  • 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-12T15:11:49+00:00Added an answer on May 12, 2026 at 3:11 pm

    I would take the advice of the code given in codeburger’s post but to add to that you have other options (as it sounds like his code is not anything new in regards to a solution).

    Possible options include:

    1. indexing on both the sender_id (INT) and recipient_id (INT) to increase search time

    2. creating a separate table for each sender_id (that way when you know who the conversation is between – assuming it is only a 1 on 1 conversation – you can just pull from two tables where the ids are common)

    3, my favorite idea, create an additional column that references a transaction database and keep a history of each transaction, indexed off the conversation number… it’s probably the “worst” in some ways, but it’s more my organizational style.

    if you have any questions, i can elaborate, in fact, i’ll probably update this later once i get home

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

Sidebar

Related Questions

I have an array containing the contents of a MySQL table. I need to
I have a MySQL table containing domain names: +----+---------------+ | id | domain |
I have a MySQL table containing a listing of events that I am trying
I have a MySQL-InnoDB table with 350,000+ rows, containing a couple of things like
I have a table watchlist containing today almost 3Mil records. mysql> select count(*) from
I have a MySQL table containing phone numbers that may be formatted in many
I have a mysql table containing a field name dtt_date and have values like
I have a simple MySQL table that is setup for full text search. |
I have a table containing last_updated_1 and last_updated_2 columns, used respectively for text and
I have a mysql table containing news data and categories and i'm trying to

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.