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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:44:08+00:00 2026-05-18T04:44:08+00:00

Possible Duplicate: SQL ORDER BY total within GROUP BY SELECT * FROM users_pm_in WHERE

  • 0

Possible Duplicate:
SQL ORDER BY total within GROUP BY

SELECT *
FROM users_pm_in
WHERE uID = '1'
GROUP BY dialog_id
ORDER BY date DESC

Wont work properly. What i want to do is group in to dialog_id.. Then all with the dialog_id sort them by date, and then sort them by date for all..

So:

id | uID | bID | msg     | dialog_id | date
--------------------------------------------------
 1 | 1   | 2   | Hello   | 1         | 1289158631
 2 | 2   | 1   | Hi?     | 1         | 1289158691
 3 | 1   | 2   | Wazzaa? | 1         | 1289158931

The two entry´s of the dialog_id gets 1 (With GROUP BY). OK. And then it should order by the one of the two entrys(inside the group) that have the newest date(order by date desc). Which this case is the one with date 1289158931.

How can this be done?

UPDATE:

What i want to come out:

while($row = mysql_fetch_array($query)){
    echo $row["msg"] // it should echo "Wazzaa?"
    echo $row["id"] // it should give me id 3
}

It give me the last for each dialog_id, thats why i want it grouped in.

  • 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-18T04:44:08+00:00Added an answer on May 18, 2026 at 4:44 am

    Use:

      SELECT a.id, 
             a.msg
        FROM USERS_PM a
       WHERE a.dialog_id = 1
         AND 1 IN (a.uid, a.bid)
    ORDER BY a.date DESC
       LIMIT 1
    

    …or:

    SELECT a.id, 
           a.msg
      FROM USERS_PM a
      JOIN (SELECT t.dialog_id,
                   MAX(t.date) AS max_date
              FROM USERS_PM t
          GROUP BY t.dialog_id) b ON b.dialog_id = a.dialog_id
                                 AND b.max_date = a.date
     WHERE a.dialog_id = 1
       AND 1 IN (a.uid, a.bid)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: SQL ORDER BY total within GROUP BY UPDATE: I've found my solution,
Possible Duplicate: SQL Select within 24 hours? I am using regular SQL to attempt
Possible Duplicate: SQL exclude a column using SELECT * [except columnA] FROM tableA? I
Possible Duplicate: SQL - how to SELECT multiple tables and JOIN multiple rows from
Possible Duplicate: SQL left join vs multiple tables on FROM line? SELECT messages.message_title, messages.message_content,
Possible Duplicate: SQL exclude a column using SELECT * [except columnA] FROM tableA? Hi
Possible Duplicate: Simple SQL Select from 2 Tables (What is a Join?) Can I
Possible Duplicate: Ordering by the order of values in a SQL IN() clause With
Possible Duplicate: Equivalent of SQL ISNULL in LINQ? I am recently migrated from ADO.Net
Possible Duplicate: SQL Server: Only last entry in GROUP BY I have a table

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.