paruby Posted May 10, 2011 Share Posted May 10, 2011 I have a table of data I want to cycle through, that includes a field that can be 1 of 4 values (an id from a 2nd table - named "locationID"). I want to query the first table order by locationID. Then for each distinct value of locationID, do specific work on those rows (could be more than 1 row returned for each locationID). My initial thought is since I know how many different locationID's there are (4), I an just do 4 different queries, 1 for each locationID (WHERE locationID = 1 , 2, etc), and do the work on each, but this does not sound clean or fast. My next thought was to do one query, get all the rows - ORDER BY locationID, then work on each distinct value of locationID - but that would possibly be a subquery... Any thoughts on the best way to do this? The work I do on each distinct locationID set is the same except for setting values. Thanks Pete Quote Link to comment https://forums.phpfreaks.com/topic/235981-cycling-through-subsets-of-a-query/ Share on other sites More sharing options...
gizmola Posted May 10, 2011 Share Posted May 10, 2011 One query, order by locationID, typical while mysql_fetch loop for the rows in your php code and use a variable that declare outside the loop which you compare to the locationID for the row you just fetched. When it changes you know it's time to do whatever special stuff you need to do for each individual locationID. Quote Link to comment https://forums.phpfreaks.com/topic/235981-cycling-through-subsets-of-a-query/#findComment-1213215 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.