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

  • Home
  • SEARCH
  • 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 6858163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:07:24+00:00 2026-05-27T02:07:24+00:00

I have two tables pertaining to this question: conversations has many messages . The

  • 0

I have two tables pertaining to this question: conversations has many messages. The basic structure (with just the relevant columns) is as follows:

conversations (
  int id (PK)
)

create table conversation_participants (
  int id (PK),
  int conversation_id (FK conversations),
  int user_id (FK users),
  unique key on [conversation_id, profile_id]
)

create table messages (
  int id (PK),
  int conversation_id (FK conversations),
  int sender_id (FK users),
  int recipient_id (FK users),
  text body
)

For each conversations entry, given a user_id I want to receive:

  • all conversations that user participated in (i.e.: conversations.*)
  • joined to the most recent matching message (i.e.: order by messages.id desc limit 1)
  • conversations ordered by their most recent message id (i.e.: order by messages.id desc)

Unfortunately, all the query help I can seem to find on anything like this pertains to MySQL, and that doesn’t work in PostgreSQL. The closest thing I found is this answer on StackOverflow that gives an example of the select distinct on (...) syntax. However, unless I’m just doing it wrong, I can’t seem to get the results ordered in the correct way given the grouping constraints I need with that method.

  • 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-27T02:07:24+00:00Added an answer on May 27, 2026 at 2:07 am

    All information is in the table “messages”, you don’t need the other tables:

    SELECT 
        id, 
        body,
        c.* -- content from conversations 
    FROM messages 
        JOIN
            (SELECT MAX(id) AS id, conversation_id 
            FROM messages 
            WHERE 1 IN(sender_id, recipient_id) -- the number is the userid, should be dynamic
            GROUP BY conversation_id) sub
            USING(id, conversation_id)
        JOIN conversations c ON c.id = messages.conversation_id
    ORDER BY
        id DESC;
    

    Edit: Just JOIN on “conversations” to get the data needed from this table.

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

Sidebar

Related Questions

I have two tables that are joined together. A has many B Normally you
I have two tables A & B, and B has a many:1 relationship with
I have two tables in a database. Table questions , has two columns, ID
I have two tables: messages (messages users posted), likes (many to many relationship between
I have two tables user and location user has id(primary) and location fields/columns location
I have two tables (renamed/refactored for illustrative purposes) with a Many-To-Many relationship in an
I have two tables Table : 1, Name : NdcAwp, Columns-Ndc, AwpUnitCost Table :
I have two tables 'Elements' and 'Lists' Lists has a primary key and a
I have two tables: A (col1,col2,col3,col4) B (col1,col2,col3,col4) Table A has 4 records(rows) and
I have two tables, items and itemmeta. items has item_id which links it 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.