hi i have an mysql query where it will return post_id by max counts of status, The result it display currently is
POST_ID
3
Mysql Table:
(`post_id`, `user_id`, `status`, `date_created`)
(2, 2, 'funny', 20121022120627)
(2, 3, 'lame', 20121023120627)
(3, 1, 'useful', 20121023120627)
(3, 3, 'lame', 20121023120627)
(3, 4, 'useful', 20121023120627)
(4, 4, 'useful', 20121024120627)
But the result i need is, to display the count from max to min on desc order.if 2 result having same number, then the top result will be based on the latest date_created similar like this
POST_ID
3
2
4
My current query is
SELECT post_id
FROM tableName
GROUP BY post_ID
HAVING COUNT(*) =
(
SELECT MAX(x.counts)
FROM
(
SELECT post_id, COUNT(*) counts
FROM tableName
GROUP BY post_id
) x
)
The sample can be found on SQLFiddle SQLFiddle
You need to
ORDER BY COUNT(*) DESC, date_created DESClike so:UPDATE 1
Try this,
Updated SQL Fiddle Demo