hello i have this SQL witch is supposed to give me the max(messages.message_id)
so i have the record where the first message_id is 7 and the last message_id is 10, but instead of giving me 10 it gives me 7… it is completely ignoring the MAX(messages.message_id) and giving me the first message_id… any suggestions on how to fix this?
SELECT
profile.first_name,
profile.last_name,
conversations.conversation_hash,
conversations.person_a,
conversations.person_b,
messages.conversation_hash,
MAX(messages.message_id),
messages.message,
messages.subject,
messages.date
FROM conversations
INNER JOIN messages
ON conversations.conversation_hash = messages.conversation_hash
INNER JOIN profile
ON profile.id = conversations.person_b
WHERE conversations.person_a = '$id'
GROUP BY messages.conversation_hash
ORDER BY messages.message_id DESC
tables:
conversations:
conversation_id | conversation_hash | person_a | person_b |
messages:
conversation_hash | from_id | to_id | message_id | subject | message | date
You can separately get the latest
message_idfrom tablemessagesinside a subquery and the result of it is then join back against the tables provided that it match on two conditions:conversation_hashandmessage_id.Full Query: