Jump to content

Cycling through subsets of a query


paruby

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/235981-cycling-through-subsets-of-a-query/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.