mysql - A multitude of the same id in an WHERE id IN () statement -


I have a simple query, which increases the value of the field by 1. Now I was looping on all the IDs and the fire seemed to be a query for each of them, but now these things are getting huge resources, I want to customize it. Normally I'll just

  update table SET field = field + 1 WHERE id (all id here)   

but now I have problems that there is ID That's twice (or more, I can not know on forehand). If the query looks like this, there is a way to run twice for ID4:

  UPDATE table SET field = field + 1 WHERE id IN (1, 2, 3, 4, 4) , 5)   

thanks,

lordstyx

Edit: Sorry for not being clear enough. ID here is an Auto Ink field, so it's all unique ID. The ID that is to be updated is coming indirectly from users, so I can not predict which ID is going to be made if IDs are (1, 2, 3, 4, 4, 5) I have to increase the line area with ID 4 to 2, and with all the rest 1.

if (1, 2, 3, 4, 4, 5) Comes with a SELECT id. query, you can do something like this:

  Add your table (Selection ID, Counter as COUNT (ID .... Group by ID) in the form of data Set your table .id = data.id yourTable.field = yourTable.field + data.counter;   

Since the input comes from users, maybe you can manipulate it in a little while. (<1> 2, 3, 4, 4, 5) to (1), (2), (3), (4), (4), (5) .

Then (a floating table is created):

  Make the table tempUpdate (ID INT);   

Take the following steps:

  1. Add values ​​to the temporary table,
  2. Run an update and
  3. Delete. INSERT (1), (2), (3), (4), (4), (5) in TempUpdate VALUES

    code:

      ; Set your data as yourTable.id = data.id in the form of your Tables (Select ID, ID (ID) from the AS counter, the template by the updating group) yourTable.field = yourTable.field + data.counter; Remove from TempUpdate;    

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -