I have an array that looks like:
array(
[0] => 1
[1] => 2
[2] => 4
[3] => 3
)
Currently I am looping over the array and running a single update along the lines of
foreach($array as $position => $id){
//query = update users set order = ($position+1) where id = $id
}
obviously not the actual code, but it get’s the point across,
If I have 1000 items in my list, that’s 1000 queries,
I was wondering if there was a way to do this all in one query?
You can use the MySQL case statement. I’m not sure whether it would be much faster or not, but here’s how it might work:
MySQL CASE Syntax