Tell me more ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Have a table photos

photos.id
photos.user_id
photos.order

A) Is it possible via a single query to group all photos by user and then update the order 1,2,3..N ?

B) added twist, what if some of the photos already have an order? Make sure that the new photos.order never gets repeated and fills in ant orders lower or higher than those existing (as best as possible)

My only thought is just to run a script on this and loop through it and re'order' everything?

EDIT:

photos.id int(10)
photos.created_at datetime
photos.order int(10)
photos.user_id int(10)

Right now data may look like this


user_id = 1
photo_id = 1
order = NULL

user_id = 2
photo_id = 2
order = NULL

user_id = 1
photo_id = 3
order = NULL

the desired result would be


user_id = 1
photo_id = 1
order = 1

user_id = 2
photo_id = 2
order = 1

user_id = 1
photo_id = 3
order = 2

share|improve this question
What is the reordering criteria? By datetime or photoname? – RolandoMySQLDBA Jun 8 '12 at 15:58
1  
Please run SHOW CREATE TABLE photos\G and display it in the question – RolandoMySQLDBA Jun 8 '12 at 15:59
1  
intially - the reordering criteria is by the date created. Ive added more schema info to the post – cgmckeever Jun 8 '12 at 21:25
this had a StackOverflow response at: stackoverflow.com/questions/10980957/… – cgmckeever Jun 12 '12 at 14:51

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.