J
J
Julia2014-06-22 11:19:42
MySQL
Julia, 2014-06-22 11:19:42

How to compose a query for multiple update mysql fields?

Hello, there is a simple table with id and float number. It is necessary to select 5 elements with the largest number, reduce it by a factor (say 100), and assign the value 1 to all other elements of the table. Is it better to organize this mass update of the table - through a loop or are there other solutions? please help me make a request.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Philip, 2014-06-22
@balista86

INSERT INTO `table` (`id_lead`,`value`)VALUES (1,'test'),(3,'hello') ON DUPLICATE KEY UPDATE value=VALUES(value)

M
Melkij, 2014-06-22
@melkij

begin;
create temporary table max5float select id as maxid from `table` order by float desc limit 5; -- проверьте что тут будет, я постоянно путаю направления сортировки
update `table` left join max5float on id=maxid set `float`=if(maxfloat is null, 1, `float`*100);
drop temporary table max5float;
commit;

The bottom line - we make a temporary table, where we throw off the IDs of the maximum 5 elements, then we update - if there is such a row, then we multiply by the coefficient, if there is no such row in the temporary table, then we assign 1.

J
Julia, 2014-06-22
@balista86

scherbanich - i.e. loop I only form the final request - right?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question