Sql 多个嵌套选择

Sql multiple nested selects

如您所见,使用此代码选择了一周的开始和结束。我是 sql 的新手,我大致了解这里发生的事情,但我无法详细解释。例如;

(select lookupweekid-1 from lookupday where dateoftransaction = date) t2 (lookupweekid)

这个让我很困惑。 (...) t2 (...), <-我不明白最后一部分的括号是什么意思

select start_of_week,end_of_week from
(select dateoftransaction
 from lookupday t1,
      (select lookupweekid-1 from lookupday 
       where dateoftransaction = date) t2 (lookupweekid)
 where t1.lookupweekid=t2.lookupweekid
   and t1.weekdaynumber=1) t3 (start_of_week)
,(select dateoftransaction
  from lookupday t1,
       (select lookupweekid-1 from lookupday 
        where dateoftransaction = date) t2 (lookupweekid)
  where t1.lookupweekid=t2.lookupweekid
  and t1.weekdaynumber=7) t4 (end_of_week)
;

t4 (end_of_week) 是一个 table 别名,它还定义了一个列别名。这避免了必须在派生的 table.

中指定列别名

所以这个:

(select dateoftransaction
 from lookupday t1 ... 
 where t1.lookupweekid=t2.lookupweekid
 and t1.weekdaynumber=7) t4 (end_of_week)

相当于:

(select dateoftransaction as end_of_week
 from lookupday t1 ... 
 where t1.lookupweekid=t2.lookupweekid
 and t1.weekdaynumber=7) t4

例如在 Postgres manual

中解释