haku Posted September 10, 2009 Share Posted September 10, 2009 Sample data: id1 id2 somecolumn 1 2 text 1 3 text 2 4 text 2 5 text 2 6 text I want to subtract one row for each id1, and the row I want to select is for the highest value of id2. So for the above table, I want rows {1, 3, text} and {2, 6, text}. id1 is non-unique, and will occur many times, but id2 will never occur more than once, even in the id1 table. Can someone give me a hand with this? I've been pondering it for a while now, and haven't been able to come up with anything. Quote Link to comment https://forums.phpfreaks.com/topic/173777-solved-query-about-a-query/ Share on other sites More sharing options...
Maq Posted September 10, 2009 Share Posted September 10, 2009 Something like this: mysql> select id1, max(id2), somecolumn from testing group by id1; +------+----------+------------+ | id1 | max(id2) | somecolumn | +------+----------+------------+ | 1 | 3 | text | | 2 | 6 | text | +------+----------+------------+ 2 rows in set (0.00 sec) Although I'm not sure what you mean by: I want to subtract one row for each id1 Quote Link to comment https://forums.phpfreaks.com/topic/173777-solved-query-about-a-query/#findComment-916057 Share on other sites More sharing options...
PFMaBiSmAd Posted September 10, 2009 Share Posted September 10, 2009 To select the actual row with a specific value in the group - http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html Quote Link to comment https://forums.phpfreaks.com/topic/173777-solved-query-about-a-query/#findComment-916060 Share on other sites More sharing options...
haku Posted September 10, 2009 Author Share Posted September 10, 2009 Maq - I meant that I wanted to select one row for each distinct value in id1, and the row needed to be the row with the highest value of id2. The link PFMaBiSmAd gave me does exactly that. Thanks PFMaBiSmAd - that's what I was looking for. Quote Link to comment https://forums.phpfreaks.com/topic/173777-solved-query-about-a-query/#findComment-916096 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.