【发布时间】:2019-04-09 03:40:44
【问题描述】:
我在加入两个表后尝试选择“contractkey”。我现在只能运行“select *”,然后返回所有列。下面是查询。只要我不尝试选择列,它就会完美运行。如何选择特定列?我需要在 INNER JOIN 之后标记表格吗?
Select *
FROM
(SELECT
attachmentdata.contractkey,
requirementinfo.reqcode,
requirementinfo.parentidkey as 'ReqInfoParentID',
max(attachment.datecreated) as datecreated,
applicationinfo.hocompletiondate
FROM location1.attachment
LEFT JOIN location1.attachmentdata
on attachment.contractkey = attachmentdata.contractkey
and attachment.id = attachmentdata.parentidkey --Links Attachment Type
LEFT JOIN location1.requirementinfo
on attachment.contractkey = requirementinfo.contractkey
and attachment.parentidkey = requirementinfo.id --Links Attachment Type
INNER JOIN location1.applicationinfo
on attachment.contractkey = applicationinfo.contractkey
and requirementinfo.parentidkey = applicationinfo.parentidkey --Links Policy Type
where
(requirementinfo.reqcode = 2)
group by attachmentdata.contractkey,requirementinfo.reqcode,
requirementinfo.parentidkey, applicationinfo.hocompletiondate) as table1
INNER JOIN
(SELECT
attachmentdata.contractkey,
requirementinfo.reqcode,
requirementinfo.parentidkey as 'ReqInfoParentID',
attachment.datecreated,
applicationinfo.hocompletiondate,
attachmentdata.pcdata
FROM location1.attachment
LEFT JOIN location1.attachmentdata
on attachment.contractkey = attachmentdata.contractkey
and attachment.id = attachmentdata.parentidkey --Links Attachment Type
LEFT JOIN location1.requirementinfo
on attachment.contractkey = requirementinfo.contractkey
and attachment.parentidkey = requirementinfo.id --Links Attachment Type
INNER JOIN location1.applicationinfo
on attachment.contractkey = applicationinfo.contractkey
and requirementinfo.parentidkey = applicationinfo.parentidkey)table2
on table1.contractkey = table2.contractkey
and table1.reqcode = table2.reqcode
and table1.ReqInfoParentID = table2.ReqInfoParentID
and table1.datecreated = table2.datecreated
and table1.hocompletiondate = table2.hocompletiondate
【问题讨论】:
-
请编辑您的问题并告诉我们如何失败。也许您在两个表之间有重复的列名。然后你需要指定你想要列的表:
select table1.foo from (...) -
是的。就是这样。我们复制的列!
标签: sql