soltek Posted April 26, 2011 Share Posted April 26, 2011 Hey there, It'd be awesome if you could help me out with this: What I'm trying to do is to select from the db all the new products of the day, BUT is there's 2 products with the same categorie, display only the last one. Something like this: SELECT id, date, txt, categorie FROM products WHERE date=$today AND [categorie is unique] ORDER by date; The part I'm having trouble with is the one between brackets. Maybe this should be done in the loop, I've no idea. Also I could use SELECT DISTINCT, but this wont allow me to select only the distincted column (categorie), right? If you could give me a hand, it'd be awesome. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/234791-select-distinct-with-more-than-one-column/ Share on other sites More sharing options...
soltek Posted April 27, 2011 Author Share Posted April 27, 2011 Afterall I can use more than one column with DISTINCT, but this will give me the rows with different combinations between the columns, and since the id number is unique... jeez! Is this impossible to do? Quote Link to comment https://forums.phpfreaks.com/topic/234791-select-distinct-with-more-than-one-column/#findComment-1206728 Share on other sites More sharing options...
soltek Posted April 27, 2011 Author Share Posted April 27, 2011 Here's an image to simplify the deal: Quote Link to comment https://forums.phpfreaks.com/topic/234791-select-distinct-with-more-than-one-column/#findComment-1206747 Share on other sites More sharing options...
Muddy_Funster Posted April 27, 2011 Share Posted April 27, 2011 if you only want to return a single entry for each catagory then: SELECT MAX(id) AS id, date, txt, categorie FROM products WHERE date=$today GROUP BY categorie Quote Link to comment https://forums.phpfreaks.com/topic/234791-select-distinct-with-more-than-one-column/#findComment-1206769 Share on other sites More sharing options...
fenway Posted April 27, 2011 Share Posted April 27, 2011 Yeah, but that's scary, since you're returning non-aggregated values in the column list, which likely makes them meaningless. Quote Link to comment https://forums.phpfreaks.com/topic/234791-select-distinct-with-more-than-one-column/#findComment-1206875 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.