suresh64633 Posted September 25, 2009 Share Posted September 25, 2009 Suppose i have a table "city" which store detail as city_id ,state_id and city name city_id state_id city_name 1 1 APO 2 1 FPO 5 1 Bethel 6 3 Indian 7 3 Anchorage 8 3 Anch 9 3 Fort Richardson If i fire a query like SELECT * FROM city GROUP BY state_id It will show single record of each group like: city_id state_id city_name 1 1 APO 5 3 Bethel But i want it must show 2 record of each group like: city_id state_id city_name 1 1 APO 2 1 FPO 6 3 Indian 7 3 Anchorage Quote Link to comment https://forums.phpfreaks.com/topic/175518-two-records-in-each-group-with-group-by-clause/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 25, 2009 Share Posted September 25, 2009 Which two do you want? The first two alphabetically? The two with the lowest city_id? Two random ones? You have got to be specific when you ask a question involving programming and data. Quote Link to comment https://forums.phpfreaks.com/topic/175518-two-records-in-each-group-with-group-by-clause/#findComment-924867 Share on other sites More sharing options...
suresh64633 Posted September 29, 2009 Author Share Posted September 29, 2009 Hi, I want first two alphabetically. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/175518-two-records-in-each-group-with-group-by-clause/#findComment-926929 Share on other sites More sharing options...
fenway Posted October 5, 2009 Share Posted October 5, 2009 That's not trivial... you'd basically need to use user variables to establish a counter for each one, and then use HAVING to filter the other ones out. It might be easier to do this in application code. Quote Link to comment https://forums.phpfreaks.com/topic/175518-two-records-in-each-group-with-group-by-clause/#findComment-930755 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.