I have a table with several hundred thousand entries and I'm trying to use a query to get a result set for a specific receiver_id and group them by sender_id. My current SQL query works but I want to know if there could be any potential problems with using two MAX calls in the statement. It looks like this:
SELECT MAX(id) as id, sender_id, receiver_id, MAX(date) as date
FROM messages
WHERE receiver_id=5 and belong_to=5
GROUP BY sender_id
The table date looks like this:
id sender_id receiver_id content date belong_to
-- --------- ----------- ------- ------------------- ---------
1 5 7 test 2013-03-11 10:33:54 7
2 5 7 test 2013-03-11 10:33:54 5
3 13 7 test 2 2013-03-13 12:01:36 7
4 13 7 test 2 2013-03-13 12:01:36 13
5 5 7 test 3 2013-03-14 09:15:37 7
6 5 7 test 3 2013-03-14 09:15:37 5
7 25 5 data1 2013-03-15 11:01:36 5
8 25 5 data1 2013-03-15 11:01:36 25
9 16 5 data2 2013-03-17 09:17:17 5
10 16 5 data2 2013-03-17 09:17:17 16
11 25 5 data3 2013-04-05 09:17:17 5
12 25 5 data3 2013-04-05 09:17:17 16
The output from my query is this:
id sender_id receiver_id date
-- --------- ----------- -------------------
9 16 5 2013-03-17 09:17:17
11 25 5 2013-04-05 09:17:17
Are there any issues with this query using the MAX calls? If so what is the alternative?