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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.