【发布时间】:2016-03-22 01:32:02
【问题描述】:
我正在尝试像以前在 MSSQL 中那样从另一个表中选择一列:
select * , Date = (select top 1 Date from [dbo].TableB where status = 1 order by Date desc)
from [dbo].TableA
如何在 PostgreSQL 中做到这一点?
附加样本数据:
表A
Names
Richards
Marcos
Luke
Matthew
John
表B
Date Status
2016-01-01 1
2016-01-02 0
2016-01-03 1
2016-01-04 1
2016-01-05 1
预期输出:
Name Date
Richards 2016-01-02
Marcos 2016-01-02
Luke 2016-01-02
Matthew 2016-01-02
John 2016-01-02
谢谢!
【问题讨论】:
-
select * , (select NewColumn from [dbo].TableB) as NewColumn不行吗? -
您能否发布示例数据和预期结果?您在
tableb中只有一条记录吗?如果不是,它将返回多个结果并很可能导致错误... -
@sgeddes- 已经编辑了我的问题。
-
@TimBiegeleisen - 是的,它在 postgresql 中不起作用。
标签: sql postgresql