Subject | Exchange two column values in a single statement... |
---|---|
Author | Milan Babuskov |
Post date | 2006-10-02T09:49:18Z |
...without a temporary field ;)
Here's how I did it:
create table t1 ( a integer, b integer );
insert into t1 (a,b) values (1,2);
insert into t1 (a,b) values (3,4);
insert into t1 (a,b) values (5,6);
insert into t1 (a,b) values (7,8);
select * from t1;
And now the magic:
update t1 set a = a+b, b = a-b, a = a-b;
select * from t1;
P.S. Kids, don't try this at home, or on important data.
</M>
Here's how I did it:
create table t1 ( a integer, b integer );
insert into t1 (a,b) values (1,2);
insert into t1 (a,b) values (3,4);
insert into t1 (a,b) values (5,6);
insert into t1 (a,b) values (7,8);
select * from t1;
And now the magic:
update t1 set a = a+b, b = a-b, a = a-b;
select * from t1;
P.S. Kids, don't try this at home, or on important data.
</M>