M
M
Michael2010-10-07 08:22:28
MySQL
Michael, 2010-10-07 08:22:28

mysql update multiple update in one query

Hello, I apologize in advance if this question seems childish to someone.

It is required to update 5 records in the same table in different rows.
Ie SET filed=1 where id=1, SET filed=2 where id=12, SET filed='0.5' where id=3…

Described as best I could. I would really appreciate it if you point me in the right direction

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
try4tune, 2010-10-07
@Legion

Your solution is slow. It is more efficient to update 1 field per 1 normal request, without joins.
Or use this option, it's much more efficient than yours:
UPDATE `table` SET `field` = CASE `id`
WHEN val1 THEN data1
WHEN val2 THEN data2
ELSE `field` END
Just be sure not to forget ELSE.

M
MuXaJIbI4, 2010-10-07
@MuXaJIbI4

you can also do this
INSERT INTO `table` (id, field) VALUES (1, 1), (2, 12), (3, 0.5) ON DUPLICATE KEY UPDATE field = VALUES(field)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question