![Share on Facebook Facebook](https://blog.lifetimecode.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png)
![Share on Twitter twitter](https://blog.lifetimecode.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png)
![Share on Reddit reddit](https://blog.lifetimecode.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png)
![Pin it with Pinterest pinterest](https://blog.lifetimecode.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/pinterest.png)
![Share on Linkedin linkedin](https://blog.lifetimecode.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png)
![Share by email mail](https://blog.lifetimecode.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png)
Every developer knows the horror of mysql sort/order by or select distinct using with group by. Mysql does group by before order by and you get mixed results not what you expected. This is a small solution with less performance problem:
SELECT * FROM
(
select * from `my_table` order by timestamp desc
) as my_table_tmp
group by catid
order by nid desc
In this example we get latest news in each category. We create a temp table by sorting by timestamp and group by after it. It worked for me.
![Share on Facebook Facebook](https://blog.lifetimecode.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png)
![Share on Twitter twitter](https://blog.lifetimecode.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png)
![Share on Reddit reddit](https://blog.lifetimecode.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png)
![Pin it with Pinterest pinterest](https://blog.lifetimecode.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/pinterest.png)
![Share on Linkedin linkedin](https://blog.lifetimecode.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png)
![Share by email mail](https://blog.lifetimecode.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png)
useful stuff 🙂
What's wrong with
select * from my_table group by catid order by catid, timestamp desc, nid desc;
?
Nothing wrong except your code will group by catid just before sorting by timestamp desc, so you will not get the latest timestamps for your grouped records.
Thanks