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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:03:49+00:00 2026-06-05T09:03:49+00:00

I have created 2 table messages and Conversation. Conversation table will create an unique

  • 0

I have created 2 table messages and Conversation.

Conversation table will create an unique id for two users who is sending and receiving messages.

Messages table will contains all info of user messages.

Conversation table

conversation_id | user_id1 | user_id2 |last_message_id
      1              5          1           4
      2              5          2           5

message_id  | conversation_id | sender_id |  message |  created_time
   1           1                   5          MSG A     2012-06-10 00:20:04
   2           1                   5          MSG B     2012-06-10 00:20:21
   3           1                   5          MSG C     2012-06-10 00:21:09
   4           1                   1          MSG D     2012-06-10 00:22:23
   5           2                   5          MSG E     2012-06-10 00:25:16

Output I want:

sender_id | message | created_time
   5        MSG E     2012-06-10 00:25:16
   1        MSG D     2012-06-10 00:22:23

What I tried:

SELECT * FROM conversation c, messages m 
WHERE c.user_id1='$userid' OR c.user_id2='$userid'
AND c.last_message_id=m.message_id
ORDER BY created_time DESC

But this query is not displaying right result.

Do we need something like following to group by as well?

SELECT * FROM TABLE
WHERE CONDITION
(SELECT MAX(created_time)
FROM TABLE
GROUP BY SOMEFEILD)
order by time desc
  • 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-05T09:03:55+00:00Added an answer on June 5, 2026 at 9:03 am

    Try to change your query like this

    SELECT * FROM conversation c, messages m 
    WHERE (c.user_id1='$userid' OR c.user_id2='$userid')
    AND c.last_message_id=m.message_id
    ORDER BY created_time DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created one table using the below command: create table Table1( Id int
I have Created a table like CREATE TABLE [dbo].[tab1]( [Id] [int] NOT NULL, [Name]
I have created a table with the following structure- $sql = CREATE TABLE followers
I have some tables like this: USERS TABLE: | id | created | active
I have two tables: messages (messages users posted), likes (many to many relationship between
I have created a messaging system for users which will allow users to send
I have this in a MySQL db: table Message sender_id int recipient_id int created
We have created a table with a trigger that updates a ModifiedDate field in
I have created a table on an Oracle 10g database with this structure :
I have created a table in postgreSQL. I want to look at the SQL

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.