D
D
Danil2017-09-15 09:03:37
MySQL
Danil, 2017-09-15 09:03:37

How to properly organize changes of 50-100 rows in mysql?

The site has a product catalog, you need to make it possible to tick 50-100 positions and change a certain characteristic of them. How can I check it in the database? Do 50-100 requests in a cycle? How right?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Evgeny Kalibrov, 2017-09-15
@rework

Do 50-100 requests in a cycle?

This is absolutely not necessary. Changes must occur within a single update request, simply form an array of the IDs of the selected products and put it in the where clause using the IN operator , for example:
UPDATE table_items
    SET option = true
    WHERE id in (11, 45, 81);

D
Dmitry Entelis, 2017-09-15
@DmitriyEntelis

If the change is the same and the structure of the database allows - a query like: If the changes are different and lines 50-100 - then separate queries. If there are 100k+ rows, you can do perversions with an intermediate table - put data into it for updating, then do update with join.

R
RidgeA, 2017-09-15
@RidgeA

If you need to do something the same for everyone - i.e. for all, set the value of one field to be the same - then update with where id in ....
otherwise - for each change a separate request;
If the database driver allows you to make several queries at the same time:
mysql.query('UPDATE ... WHERE ID = 1; UPDATE ... WHERE ID = 2;....'. ....)

A
Alexey Shashenkov, 2017-09-15
@teknik2008

UPDATE "my-table" AS t
  SET "val"=v."val",
    "msg"=v."msg"
   FROM (VALUES(1,123,'hello'),(2,456,'world!'))
   AS v("id","val","msg") WHERE v.id = t.id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question