简体   繁体   中英

PHP/SQL: Select one per dialog_id

Ok, in my PM income list I have this:

$queryIn = "SELECT * FROM users_pm_in WHERE uID = '$USER' ";

Take's all queries and shows them. Now everytime user creates a PM, he creates a "dialog_id" number. If the recipient respond to the user´s PM, the recipient´s PM will also have that number in dialog_id.

So, that's why I would only want to show 1 entry in the income, per dialog_id. How it looks in users_pm_in:

id | uID | bID | msg | dialog_id
--------------------------------
 1 | 1 | 2 | Hello | 1
 2 | 2 | 1 | Hi? | 1
 3 | 1 | 2 | Wazzaa? | 1

uID is who is receiving the message, bID is the user who sended it, msg is message, dialog_id shows that they are writing in the same dialog all the same(they responded eachother).

So if I am id 1, in my income list I would have 2 entrys right now, with the query above.

What I want to have, is only 1 IF it's the same dialog (dialog_id and same bID)

And the 1 that it is showing should be the one order by date desc

SELECT * FROM users_pm_in WHERE uID = '$USER' GROUP BY dialog_id ORDER BY id DESC

I think the problem is that you can't guarantee which row MySQL will return after the GROUP BY statement -- you want it to return the row with the 'highest' (ie most recent) date, but at the moment it might not necessarily return that. Accordingly:

SELECT *, MAX(date) FROM users_pm_in WHERE uID = '$USER' GROUP BY dialog_id ORDER BY date DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM