11

我想Select在 Postgresql 中加入一个 Statement,但我遇到了问题

SELECT 
    s.sessionid, sp.lang 
FROM 
    sessions s
INNER JOIN 
    pages sp 
ON 
    sp.sessionid =  s.sessionid 
INNER JOIN
(
    SELECT 
        max(sessionid)
    FROM 
        sessions
    AS 
        maxSession
)
ON 
    maxSession = s.sessionid
WHERE 
    --Where condition

我收到以下错误:错误:FROM 中的子查询必须有别名

LINE 6: (
        ^
HINT:  For example, FROM (SELECT ...) [AS] foo.

如果我添加FROM

FROM
(
    SELECT max(sessionid)
    FROM sessions
)
AS maxSession

我收到另一个错误

ERROR:  syntax error at or near "FROM"
LINE 7:  FROM

想法?

4

1 回答 1

14

你很亲密。

  INNER JOIN
 (
  SELECT 
    max(sessionid) as 'maxSession'
   FROM 
   sessions        
) maxSession
ON 
maxSession.maxSession = s.sessionid

每当您将查询称为表时,都需要给它一个别名...别名紧跟在整个子查询之后,而不是在子查询本身中。

于 2013-09-23T21:51:58.137 回答