yamipoli Posted January 9, 2011 Share Posted January 9, 2011 sorry for bad phrasing but I don't know how else to explain it. ----------------------------- | Fruit | Status | ----------------------------- | Apple | Bitten | ----------------------------- | Apple | Whole | ----------------------------- | Orange | On Fire | ----------------------------- | Orange | Not On Fire | ----------------------------- | Lemon | On A Tricycle | ----------------------------- What I want to do is find the first X fruits (assume X = 2 for this), and each row that has that key as well. So for example I want the first four rows because they are 'apple' and 'orange'. I can't specify based on Fruit name or any such easy ways out. Quote Link to comment https://forums.phpfreaks.com/topic/223834-pull-out-first-x-unique-fields-records-and-rows-associated-with-them/ Share on other sites More sharing options...
smerny Posted January 9, 2011 Share Posted January 9, 2011 i'm not sure if there is a way to do it with SQL, there probably is, but with PHP you could do it.. i'd do something like... $last_fruit = ""; $num_fruits = 0; while($num_fruits < 3){ $row = mysql_fetch_assoc($result) if($row['fruit'] != $last_fruit){ $last_fruit = $row['fruit']; $num_fruits++; } if($num_fruits < 3){ //whatever you want to do with it } } of course this is after whatever sql returns the above table... maybe not how others would do it, but it would serve your purpose Quote Link to comment https://forums.phpfreaks.com/topic/223834-pull-out-first-x-unique-fields-records-and-rows-associated-with-them/#findComment-1156920 Share on other sites More sharing options...
yamipoli Posted January 9, 2011 Author Share Posted January 9, 2011 Actually I have a way with php already, but I was afraid I was missing something simple basically and wanted to check. Quote Link to comment https://forums.phpfreaks.com/topic/223834-pull-out-first-x-unique-fields-records-and-rows-associated-with-them/#findComment-1156934 Share on other sites More sharing options...
smerny Posted January 9, 2011 Share Posted January 9, 2011 ah.. i'd think to do something like SELECT * FROM fruit_table WHERE FRUIT IN( SELECT FRUIT FROM fruit_table GROUP BY FRUIT LIMIT 2) but mysql doesn't allow limits within subqueries, and i'm not sure what the alternative is Quote Link to comment https://forums.phpfreaks.com/topic/223834-pull-out-first-x-unique-fields-records-and-rows-associated-with-them/#findComment-1156941 Share on other sites More sharing options...
yamipoli Posted January 9, 2011 Author Share Posted January 9, 2011 Hmmm... alright then, is there just a way to see how many of each unique fields there are (like, getting back a list like apple: 2, orange:2 lemon:1 ? i might could use that instead of I recode some stuff. Quote Link to comment https://forums.phpfreaks.com/topic/223834-pull-out-first-x-unique-fields-records-and-rows-associated-with-them/#findComment-1156944 Share on other sites More sharing options...
smerny Posted January 9, 2011 Share Posted January 9, 2011 SELECT Fruit, COUNT( * ) AS count FROM fruit_table GROUP BY Fruit Quote Link to comment https://forums.phpfreaks.com/topic/223834-pull-out-first-x-unique-fields-records-and-rows-associated-with-them/#findComment-1156953 Share on other sites More sharing options...
yamipoli Posted January 9, 2011 Author Share Posted January 9, 2011 Thank you muchly for your help Quote Link to comment https://forums.phpfreaks.com/topic/223834-pull-out-first-x-unique-fields-records-and-rows-associated-with-them/#findComment-1156965 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.