optikalefx Posted January 14, 2011 Share Posted January 14, 2011 Hi, I'm usually pretty skilled at mysql, but this stupid simple problem i must be overlooking. I have a store with a bunch of orders in it. I want to get a result of the most recent purchases, but don't get duplicates. So if the purchases are p1, p2, p2, p2, p2, p3, p4 im trying to get p1, p2, p3, p4 as my result This SELECT products_name FROM orders_products ORDER BY orders_products_id desc gives me all results but this SELECT distinct products_name FROM orders_products ORDER BY orders_products_id desc gives me totally different results. it gives me like p4, p6, p9p, p15. group by gives me the same. any ideas? i hope i explained that correctly. Quote Link to comment https://forums.phpfreaks.com/topic/224450-how-to-get-at-least-1-distinct-result/ Share on other sites More sharing options...
Maq Posted January 14, 2011 Share Posted January 14, 2011 Let me clarify. You want to get the latest purchase for each p#? Quote Link to comment https://forums.phpfreaks.com/topic/224450-how-to-get-at-least-1-distinct-result/#findComment-1159466 Share on other sites More sharing options...
optikalefx Posted January 14, 2011 Author Share Posted January 14, 2011 yes. I thought grouping by p# would solve it, but it ruins the order. Quote Link to comment https://forums.phpfreaks.com/topic/224450-how-to-get-at-least-1-distinct-result/#findComment-1159470 Share on other sites More sharing options...
Maq Posted January 14, 2011 Share Posted January 14, 2011 There may be a shorter way but this should work: SELECT products_name FROM orders_products WHERE orders_products_id IN(SELECT MAX(orders_products_id) FROM orders_products GROUP BY products_name); (I tried the GROUP BY ORDER BY and it didn't work, same with DISTINCT.) Quote Link to comment https://forums.phpfreaks.com/topic/224450-how-to-get-at-least-1-distinct-result/#findComment-1159474 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.