daydreamer Posted November 16, 2009 Share Posted November 16, 2009 Hi, I have a table like this: ---Prices--- Price Product ID Supplier ID Timestamp 44 1 2 45 1 2 40 1 3 I am trying to select the latest price for each supplier for a given product ID. This is what I have: $result = mysql_query("SELECT * FROM prices WHERE product_id='$id' ORDER BY price desc")or die(mysql_error()); I have tried a few things like group by, and distinct but cannot get the exact data set I need. How would I do this? Quote Link to comment https://forums.phpfreaks.com/topic/181671-solved-select-row-that-has-a-unique-value-and-max-timestamp/ Share on other sites More sharing options...
JustLikeIcarus Posted November 16, 2009 Share Posted November 16, 2009 Ummm.. Have you tried..? select supplier_id, price, max(timestamp) from prices where product_id = ? group by supplier_id, price Quote Link to comment https://forums.phpfreaks.com/topic/181671-solved-select-row-that-has-a-unique-value-and-max-timestamp/#findComment-958434 Share on other sites More sharing options...
PFMaBiSmAd Posted November 16, 2009 Share Posted November 16, 2009 To get the actual row (so that you would get the correct information from other columns in it, like the price or an id) holding a specific value (max, min...) for a group, see this link - http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html Quote Link to comment https://forums.phpfreaks.com/topic/181671-solved-select-row-that-has-a-unique-value-and-max-timestamp/#findComment-958438 Share on other sites More sharing options...
kickstart Posted November 16, 2009 Share Posted November 16, 2009 Hi To bring them all back, do a SELECT to bring back the latest timestamp for each produce, grouped by product id and supplier id, then JOIN that to the original table on the product id, supplier id and timestamp to get the rest of the fields (such as the actual price). Something like this:- SELECT a.Price, a.product_id, a.supplier_id, b.MaxTimeStamp FROM Prices a JOIN (SELECT product_id, supplier_id, Max(Timestamp) AS MaxTimeStamp FROM Prices GROUP BY product_id, supplier_id) b ON a.product_id . b.product_id AND a.supplier_id = b.supplier_id AND a.Timestamp = b.MaxTimeStamp You can then narrow this down to a particular product id with a WHERE clause (either at the end, or within the subselect). All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/181671-solved-select-row-that-has-a-unique-value-and-max-timestamp/#findComment-958442 Share on other sites More sharing options...
daydreamer Posted November 17, 2009 Author Share Posted November 17, 2009 Thanks for the link PFMaBiSmAd, did look at that page wasnt sure if I was able to use that method. SELECT a.Price, a.product_id, a.supplier_id, b.MaxTimeStamp FROM Prices a JOIN (SELECT product_id, supplier_id, Max(Timestamp) AS MaxTimeStamp FROM Prices GROUP BY product_id, supplier_id) b ON a.product_id . b.product_id AND a.supplier_id = b.supplier_id AND a.Timestamp = b.MaxTimeStamp Thanks alot this worked a treat Quote Link to comment https://forums.phpfreaks.com/topic/181671-solved-select-row-that-has-a-unique-value-and-max-timestamp/#findComment-958797 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.