http://my.oschina.net/kenshiro/blog/160129
http://my.oschina.net/Kenyon/blog/55137
注意with和紧接着的一个sql语句是一个整体
我们使用数据修改语句,以及WITH子句编写一个查询,如下所示。首先创建一个类似于表公司表COMPANY1。查询在这个例子中,有效地移动行,从COMPANY到COMPANY1。删除删除指定的公司行RETURNING子句返回它们的内容;然后主查询读取输出,并将其插入到COMPANY1表:
CREATE TABLE COMPANY1( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL AGE INT NOT NULL ADDRESS CHAR(50), SALARY REAL ); WITH moved_rows AS ( DELETE FROM COMPANY WHERE SALARY >= 30000 RETURNING * ) INSERT INTO COMPANY1 (SELECT * FROM moved_rows); www.yiibai.com
以上Postgresql的表会产生以下结果:
INSERT 0 3
现在的记录表COMPANY与COMPANY1如下:
testdb=# SELECT * FROM COMPANY; id | name | age | address | salary ----+-------+-----+------------+-------- 1 | Paul | 32 | California | 20000 2 | Allen | 25 | Texas | 15000 3 | Teddy | 23 | Norway | 20000 7 | James | 24 | Houston | 10000 (4 rows) testdb=# SELECT * FROM COMPANY1; id | name | age | address | salary ----+-------+-----+-------------+-------- 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 6 | Kim | 22 | South-Hall | 45000 原文链接:https://www.f2er.com/postgresql/194619.html