How can a single UPDATE swap two feature columns?
SQLite evaluates right-hand SET expressions before assignment, allowing the two original column values to be exchanged.
SQLite example and interpretation
A misfiled width and height of three and eight become eight and three. Scope a real correction to affected rows so valid dimensions are preserved.
CREATE TABLE d(id INTEGER,w INTEGER,h INTEGER);
INSERT INTO d VALUES(1,3,8);
UPDATE d SET w=h,h=w WHERE id=1;
SELECT w,h FROM d;Result [[8, 3]]
中文
如何在一条 UPDATE 中交换两个特征列?
SQLite 在赋值前计算 SET 右侧表达式,因此同一行中两个旧值可用于互换。
SQLite 示例与适用条件
误将宽和高存反,单条更新可把 3、8 换成 8、3。真实数据修正要限定受影响行,避免把正确记录也交换。
CREATE TABLE d(id INTEGER,w INTEGER,h INTEGER);
INSERT INTO d VALUES(1,3,8);
UPDATE d SET w=h,h=w WHERE id=1;
SELECT w,h FROM d;结果 [[8, 3]]