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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:06:56+00:00 2026-06-12T15:06:56+00:00

Im developing a chat tool and im having problems with the sql returning the

  • 0

Im developing a chat tool and im having problems with the sql returning the right results with group by and order by. The table has a structure shown below;

tablename: chat

id | from | to | sent | read | message

‘from’ and ‘to’ are a signed integers (userids)
‘sent’ is a timestamp when the message was sent.
‘message’ is text msg
‘read’ is an int (0 for unread, 1 for read).

im trying to return a list of the most recent messages grouped by users

eg

id         from     to      message        sent    read
7324       21      1       try again    1349697192  1
7325       251       1     yo whats up  1349741502  0
7326       251       1     u there      1349741686  0   

should return this after the query

    id      from    to     message      sent        read
7326        251     1        u there    1349741686   0
7324        21      1       try again    1349697192  1

here is my query

$q ="SELECT chat.to,chat.read,chat.message,chat.sent,chat.from FROM `chat` WHERE chat.to=$userid GROUP BY chat.from ORDER BY chat.sent DESC,chat.read ASC LIMIT ".(($page-1)*$count).",$count";            

it doesnt return the desired results;

  • 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-12T15:06:58+00:00Added an answer on June 12, 2026 at 3:06 pm

    You should create a subquery which determines the latest sent by users then join it with chat table.

    SELECT  a.*                   -- this will list all latest rows from chat table
    FROM    `chat` a
            INNER JOIN
            (
                SELECT `from`, `to`, MAX(sent) maxSent
                FROM `chat`
                GROUP BY `from`, `to`
            ) b ON a.`from` = b.`from` AND
                    a.`to` = b.`to` AND
                    a.sent = b.maxSent
    -- WHERE ....                 -- add your condition(s) here
    

    SQLFiddle Demo

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

Sidebar

Related Questions

(I am developing an app that presents chat messages in a table. But, this
I am developing a chat application. But Right now chatting is possible with only
I am developing a one chat application. in which first screen has one button
I am developing a group chat application to learn how to use sockets, threads
I'm developing a chat application in java using DatagramSocket and DatagramPacket . The data
I am developing a chat website using jsp/servlet.I will be hosting my website on
Hi all i am developing a chat application ... i have multiple chat windows
I am using libpurple (libpurple.com) for developing a chat application. Everything works fine on
I'm developing a small chat application between client and server. I would like to
I am developing an app that needs the chat functionality, presenting messages sent and

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.