Bo asked to see the park in a blog this question: a table has two fields id and name, id is a continuous non-empty non-repetition, want to exchange value of the adjacent table name records (single and double twenty-two exchange ).
Further, if the last row does not correspond to a single matching record, then the next row, not the last update individual rows.
Feel a bit mean, I try to achieve it (SQL Server), and recorded.
Create a diosos table.
- If the table exists, it is deleted table IF object_id (N ' diosos ' , N ' the U- ' ) IS Not null drop Table diosos; - Create Table Create Table diosos (ID int , name VARCHAR ( 64 ));
Inserting data into a table.
-- 插入数据 insert into diosos(id, name) values(1, 'one'); insert into diosos(id, name) values(2, 'two'); insert into diosos(id, name) values(3, 'three'); insert into diosos(id, name) values(4, 'four'); insert into diosos(id, name) values(5, 'five'); insert into diosos(id, name) values(6, 'six'); insert into diosos(id, name) values(7, 'seven'); insert into diosos(id, name) values(8, 'eight'); insert into diosos(id, name) values(9, 'nine');
Query data is not out to see for the first.
- query data the SELECT * from diosos;
Achieve neighbor list records exchange (Mono twenty-two exchange), the principle is based on the name and the parity judgment id modulo subtraction do id adjacent left outer association of updating rows.
- Update, between two exchange, the exchange is not unpaired Update D1 SET d1.name = d2.name from diosos D1 left the Join diosos D2 ON (d1.id % 2 = 0 and d1.id = (d2.id + . 1 ) ) or (d1.id % 2 = . 1 and d1.id = (d2.id - . 1 )) WHERE d2.name IS Not null ;
Query again, successfully.
- re-query the SELECT * from diosos;
Still quite good fun, although I think there is almost no actual scene, ha ha ha.
"You have to believe in this world someone will know the pain behind you, and people are willing to give you a brilliant throughout the season."