I have a weird problem. When I execute a query the data is not displayed in the Resultset however when I click on the column header in the ResultSet to sort it, the row appears.
My table looks like this –
id user_id action_id object_id MAX(s.timestamp)
2825 61 2 806 16/06/2012 03:41:55
2818 208 4 0 15/06/2012 15:11:30 -- problematic row
2817 21 7 1 15/06/2012 13:18:38
2816 208 4 0 15/06/2012 12:11:30 -- problematic row
2803 320 9 806 14/06/2012 23:14:32
2802 320 9 805 14/06/2012 17:15:54
2801 208 4 0 14/06/2012 15:11:30 -- problematic row
I am using the following query –
SELECT MAX(activity_stream_id) id,
s.user_id,
s.action_id,
s.object_id,
MAX(s.timestamp)
FROM pp_activitystream s
GROUP BY s.user_id, s.action_id, s.object_id
ORDER BY s.timestamp DESC
Ideally, in the ResultSet I should get the top most of the “problematic row” but i do not.
When I click on the column header to sort the row, the “problematic row” appears.
Can you tell me why is it behaving like this?
I executed the above query using Toad and MySQL Workbench and it behaves the same way.
Output after execution –
id user_id action_id object_id MAX(s.timestamp)
2825 61 2 806 16/06/2012 03:41:55
2817 21 7 1 15/06/2012 13:18:38
2803 320 9 806 14/06/2012 23:14:32
2802 320 9 805 14/06/2012 17:15:54
Output after clicking on any column header to sort (timestamp in this case) –
id user_id action_id object_id MAX(s.timestamp)
2825 61 2 806 16/06/2012 03:41:55
2818 208 4 0 15/06/2012 15:11:30 -- row appears
2817 21 7 1 15/06/2012 13:18:38
2803 320 9 806 14/06/2012 23:14:32
2802 320 9 805 14/06/2012 17:15:54
Mysql lets you use non-
grouped columns amongselected columns, and non-selected columns inorder byclause. Even if that sometimes might feel sexy or useful (at least to me), it is conceptually flawed, and may lead to errors very difficult to spot.In this case you are trying to order on a column that appears as an aggregate column in select, leading to unpredictable results. When you click on the resultset table header in toad or mysql-workbench, you actually telling mysql to order on
MAX(timestamp), making things work fine.