以mysql查询运行
UPDATE counter SET total = 1 WHERE id = 406;
UPDATE counter SET total = 3 WHERE id = 407;
UPDATE counter SET total = 2 WHERE id = 408;
UPDATE counter SET total = 7 WHERE id = 409;
UPDATE counter SET total = 1 WHERE id = 410;
我只能通过将相同的总值分组如下来优化上面的查询:
UPDATE counter
SET total = 1
WHERE name IN (406, 410);
有没有办法更好地优化它,而不是逐个执行(循环)更新查询。
答案 0 :(得分:2)
你需要这个:
UPDATE counter SET total = CASE
WHEN id = 406 THEN 1
WHEN id = 407 THEN 3
WHEN id = 408 THEN 2
WHEN id = 409 THEN 7
WHEN id = 410 THEN 1
END
答案 1 :(得分:0)
您可以使用键值对进行更新,如下所示
UPDATE counter SET total = '".$value."' WHERE id = '".$key."';