我想知道,是否有可能使用PostgreSQL加入查询结果?
答案 0 :(得分:34)
您可以使用WITH:
执行此操作WITH subquery AS(
SELECT * FROM TheTable
)
SELECT *
FROM subquery q1
JOIN subquery q2 on ...
或者通过创建包含查询的VIEW,然后加入:
SELECT *
FROM TheView v1
JOIN TheView v2 on ...
或蛮力方法:键入子查询两次:
SELECT *
FROM (
SELECT * FROM TheTable
) sub1
LEFT JOIN (
SELECT * FROM TheTable
) sub2 ON ...
答案 1 :(得分:2)
你的意思是,对表的查询结果,对同一个表。如果是,那么是的,这是可能的......例如。
--Bit of a contrived example but...
SELECT *
FROM Table
INNER JOIN
(
SELECT
UserID, Max(Login) as LastLogin
FROM
Table
WHERE
UserGroup = 'SomeGroup'
GROUP BY
UserID
) foo
ON Table.UserID = Foo.UserID AND Table.Login = Foo.LastLogin
答案 2 :(得分:0)
是的,只需查询别名:
SELECT *
FROM (
SELECT *
FROM table
) t1
JOIN (
SELECT *
FROM table
) t2
ON t1.column < t2.other_column