PostgreSQL的CTE

在看PostgreSQL的源代码的时候,总是看到 CTE。

所谓CTE,就是 common table express。

这里有一个小例子:

WITH test(x) AS (SELECT 1 UNION SELECT 2)
SELECT *  FROM test;

这个是官方说明:

http://www.postgresql.org/docs/9.2/static/queries-with.html

WITH provides a way to write auxiliary statements for use in a larger query. These statements, which are often referred to as Common Table Expressions or CTEs, can be thought of as defining temporary tables that exist just for one query. 

其目的,是为了简化SQL文的书写,让它至少看上去更加有条理,更加清晰。

原文地址:https://www.cnblogs.com/gaojian/p/3120464.html