0

I have this db table:

Items
-------
id  // values: 1,2,3,4,5 ... 
name // values: Item1,Item2,Item3,Item4,Item5 ...
order // values: 1, 5, 2, 3, 4 ...

The items are images. The image order is set in an array when dragging the images around, but in order to update the order, I use one UPDATE inside a loop. I was wondering if I could do the UPDATE without using a loop.

I was wondering if I can update the item's order at once, using only one UPDATE query.

3
  • Depends what you want the value of order to be. If they're just supposed to be the same as the id you can do UPDATE Items SET order = id but I doubt thats what you want. Please clarify. Commented Mar 14, 2014 at 15:37
  • The items are images. The image order is set in an array when dragging the images around, but in order to update the order, I use one UPDATE inside a loop. I was wondering if I could do the UPDATE without using a loop. Commented Mar 14, 2014 at 15:40
  • The order will be updated to the new order set. Please review the post, I did an edit Commented Mar 14, 2014 at 15:44

1 Answer 1

1

You can use INSERT (..) ON DUPLICATE KEY UPDATE (..) syntax.

INSERT INTO `table` (`id`, `order`)
VALUES (1, 1), (2, 5), (3, 2), (4, 3), (5, 4)
ON DUPLICATE KEY UPDATE `order` = VALUES(`order`)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.