tags:

views:

57

answers:

3

I'm using two tables for selecting messages between users. Table "messages" for recording the messages and table "members" for checking users data (are they activ or deleted).

This query working fine.

What I need to do is list the received messsages order by last received. I tried to do with "ORDER BY messages.id DESC" at the end of this query but it didn't work. All messages are listed from first received.

This is the mysql join table query that I'm using:

sql = "SELECT DISTINCT messages.fromid,
            messages.readed,
            messages.fromid,
            messages.toid  ,
            members.id AS pid
FROM            messages
INNER JOIN members
ON              members.id = messages.fromid
WHERE           messages.toid              =  ".$mid."
AND             members.status             = 7
AND             messages.kreaded          !='1'
AND             messages.subject          != 'readed'
GROUP BY        fromid"

Is there any way to do this?

alt text

A: 

Do you have a column in the table for "received" or a column with some sort of timestamp? If you do, you should order by that.

If not, please post the entire table structure so we can better help you.

Kevin Crowell
Yes there is column "sendtime" and I tried with "ORDER BY messages.sendtime DESC" but nothing changes.
Sergio
Did you select messages.sendtime also? If it is not selected in your query, you cannot order by it.
Kevin Crowell
yes I did but no effect
Sergio
We need to see your exact queries and sample output. Just saying that you tried and it doesn't work will not cut it. We believe it should work, so we need to see WHY it didn't work.
Kevin Crowell
Ok, this is the exact query: select distinct messages.fromid, messages.id,messages.toid,messages.sendtime,messages.message,messages.subject,messages.id as porid,members.id as pid From messages Inner Join members on members.id = messages.fromid Where messages.toid = $mid AND members.status = 7 AND messages.subject != 'readed' GROUP BY fromid ORDER BY messages.fromid ASC, messages.sendtime DESC
Sergio
The output of this query showing the table with users (group by FROMID) who sent message to the user toid. The problem is that the message listed below the name of the user who sent message is not the last one but the first message.
Sergio
Are you sure that this query even works. You are using "group by" but you do not have an aggregate function (such as sum) anywhere. Try leaving out the group by part of the query, and only order by messages.sendtime DESC. Do not order by messages.fromid.
Kevin Crowell
Yes it's working. I will put screenshot of PhpMyAdmin. Maybe then will be easier for me to explain what I need to do...
Sergio
Did you try my suggestion?
Kevin Crowell
I tried with the column sendtime and it did not work...it still show the first message sent not the last one
Sergio
If you can see the sreenshot now, column ID (message.id) show the id of the first message sent not the last one...and I need to get the last message sent
Sergio
+1  A: 

How about sonething like

SELECT DISTINCT messages.fromid, 
            messages.readed, 
            messages.fromid, 
            messages.toid  , 
            members.id AS pid 
FROM            messages 
INNER JOIN members 
ON              members.id = messages.fromid 
WHERE           messages.toid              =  ".$mid." 
AND             members.status             = 7 
AND             messages.kreaded          !='1' 
AND             messages.subject          != 'readed'
ORDER BY    messages.fromid ASC,
            messages.id DESC

OR

SELECT DISTINCT messages.fromid, 
            messages.readed, 
            messages.fromid, 
            messages.toid  , 
            members.id AS pid 
FROM            messages 
INNER JOIN members 
ON              members.id = messages.fromid 
WHERE           messages.toid              =  ".$mid." 
AND             members.status             = 7 
AND             messages.kreaded          !='1' 
AND             messages.subject          != 'readed'
ORDER BY    messages.fromid ASC,
            messages.sendtime DESC

In messages.fromid ASC the ASC is purely syntactically, as ORDER BY without specifying ASC/DESC assumes ASC.

SELECT *
FROM TABLE
ORDER BY COL ASC

is the same as

SELECT *
FROM TABLE
ORDER BY COL
astander
Nope. I tried both of this. It's always listing the first message at the top.
Sergio
**You are aware tha twhat you are saying makes almost no sense what so ever.** Show us some input data, and expexted output, and maybe we can have a look then...
astander
A: 

I'm no sql expert, by when you tried your ORDER BY clause, did you order by messages.id or the alias "pid" you declared in your statement? Might that have an effect on the order by not working as expected?

kevtrout