修改SQL语句以从表中追加列

修改SQL语句以从表中追加列,sql,database,postgresql,pgadmin,Sql,Database,Postgresql,Pgadmin,我有两个表:TrainingMatrix和Data。对于TrainingMatrix我有以下SQL语句: SELECT DISTINCT ON (payroll, "TrainingName", "Institute") * FROM "TrainingMatrix" ORDER BY payroll, "TrainingName", "Institute" ,"TrainingDate" DESC NULLS LAST; 每个工资单编号

我有两个表:
TrainingMatrix
Data
。对于
TrainingMatrix
我有以下SQL语句:

SELECT DISTINCT ON (payroll, "TrainingName", "Institute") *
            FROM   "TrainingMatrix" 
            ORDER  BY payroll, "TrainingName", "Institute" ,"TrainingDate" DESC NULLS LAST;
每个工资单编号都与唯一的员工姓名相关。因此,在
Data
表中,我有两列:
payroll
“EmployeeName”
。如何修改前面的语句,以便显示/project
“EmployeeName”
,同时显示结果


我使用PostgreSQL 9.2和pgAdmin III。

您可以将查询与
数据表连接起来:

SELECT DISTINCT ON ("TrainingMatrix".payroll, "TrainingName", "Institute") "Data".EmployeeName, "TrainingMatrix".*
FROM "TrainingMatrix" 
JOIN "Data" ON "TrainingMatrix".payroll = "Data".payroll
ORDER  BY payroll, "TrainingName", "Institute" ,"TrainingDate" DESC NULLS LAST;

您可以将查询与
数据
表连接起来:

SELECT DISTINCT ON ("TrainingMatrix".payroll, "TrainingName", "Institute") "Data".EmployeeName, "TrainingMatrix".*
FROM "TrainingMatrix" 
JOIN "Data" ON "TrainingMatrix".payroll = "Data".payroll
ORDER  BY payroll, "TrainingName", "Institute" ,"TrainingDate" DESC NULLS LAST;

谢谢亲爱的,你的意思是
数据
而不是
Fata
:)。还有一件事,我怎样才能把
EmployeeName
作为显示的第一列?谢谢您的注意,@Aan。我已经确定了我的答案,以解决您的更正和补充问题。谢谢,亲爱的,您的意思是
数据
而不是
Fata
:)。还有一件事,我怎样才能把
EmployeeName
作为显示的第一列?谢谢您的注意,@Aan。我已经确定了我的答案,以解决您的更正和补充问题。