Jump to content

how to update infinite amount of rows in 1 query?


Monkuar

Recommended Posts

AFAIK to do it in one query, you would need to dynamically produce the query statement using CASE logic (untested) -

 

UPDATE your_table SET rank = CASE WHEN friend = 'shiskabob' THEN 2 WHEN friend = 'test2' THEN 0 WHEN friend = 'monkey' THEN 0 END WHERE id = x

 

hmm sounds mysql intensive

 

do u recommend me just creating a button for a user to click to auto move the rank up/down each time? seems better and better performance? do u agree or no?

Link to comment
Share on other sites

AFAIK to do it in one query, you would need to dynamically produce the query statement using CASE logic (untested) -

 

UPDATE your_table SET rank = CASE WHEN friend = 'shiskabob' THEN 2 WHEN friend = 'test2' THEN 0 WHEN friend = 'monkey' THEN 0 END WHERE id = x

 

hmm sounds mysql intensive

 

do u recommend me just creating a button for a user to click to auto move the rank up/down each time? seems better and better performance? do u agree or no?

 

MySQL won't even bat an eyelash at that.

 

You could also just use multiple queries. In this case it won't hurt anything.

Link to comment
Share on other sites

AFAIK to do it in one query, you would need to dynamically produce the query statement using CASE logic (untested) -

 

UPDATE your_table SET rank = CASE WHEN friend = 'shiskabob' THEN 2 WHEN friend = 'test2' THEN 0 WHEN friend = 'monkey' THEN 0 END WHERE id = x

 

hmm sounds mysql intensive

 

do u recommend me just creating a button for a user to click to auto move the rank up/down each time? seems better and better performance? do u agree or no?

 

MySQL won't even bat an eyelash at that.

 

You could also just use multiple queries. In this case it won't hurt anything.

 

could you help me run a foreach then? if i could do that with my query, even if somone selcted let's say 10 checkboxes, it will run 10 update queries, but it wont effect performance? if so that is great news, if you could help me

Link to comment
Share on other sites

could you help me run a foreach then? if i could do that with my query, even if somone selcted let's say 10 checkboxes, it will run 10 update queries, but it wont effect performance? if so that is great news, if you could help me

 

More queries will be more time, always, but you probably wont notice a difference til you start doing several hundred in a loop.  I had a script at one point that updated users achievement data and it was doing anywhere between 700-1500 updates in a loop.  Rewriting it do a single query using a giant case statement made a noticeable increase in performance.

 

As for setting up your loop, that is simple.  Just make a standard foreach loop to go through your boxes.  In that loop, create and run your query.

foreach ($ibforums->input['rank'] as $rank){
   $sql = 'UPDATE ....';
   //run query $sql;
}

 

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.