我发现了用一个查询更新 2 个 mysql 行的这种语法。它看起来像很好的逻辑,但是我无法让它按照我想要的方式工作。
如果有人能告诉我这种语法是否错误,我将不胜感激。我是否正确理解这一点?
$id_active = 1;
$id_swap = 2;
UPDATE article_test
// the column to update is 'sort_id' and since it is also the column I need to test, then I use the same value for CASE right?
SET sort_id = CASE sort_id
WHEN $id_active THEN $id_swap // sort_id 1 to become sort_id 2
WHEN $id_swap THEN $id_active // sort_id 2 to become sort_id 1
WHERE sort_id IN ($id_swap,$id_active) // test only rows with sort_id 1 & sort_id 2