【发布时间】:2020-08-02 06:17:14
【问题描述】:
我对 SQL 选择相当陌生,我正在尝试从多个表中进行选择,以显示 sync_lwcf_annual_inspection_report_for_active_grants 表中的所有结果,并在匹配值的位置附加其他表中的列。如果没有找到匹配项,只要它显示为 null 并且 sync_lwcf_annual_inspection_report_for_active_grants 没有被省略或重复,就可以了。下面是我尝试过的一些代码。此外,我尝试使用一些左外连接但没有成功。任何帮助将不胜感激。
SELECT
sync_lwcf_annual_inspection_report_for_active_grants.projectnum,
sync_lwcf_annual_inspection_report_for_active_grants.projectnumother,
sync_lwcf_annual_inspection_report_for_active_grants.projecttitle,
sync_lwcf_annual_inspection_report_for_active_grants.projectother,
sync_lwcf_annual_inspection_report_for_active_grants.annual_inspect,
sync_lwcf_annual_inspection_report_for_active_grants.inspect_date,
sync_lwcf_annual_inspection_report_for_active_grants.past_assistance,
sync_lwcf_annual_inspection_report_for_active_grants.public_rec,
sync_lwcf_annual_inspection_report_for_active_grants.park_maintained,
sync_lwcf_annual_inspection_report_for_active_grants.lwfc_sign,
sync_lwcf_annual_inspection_report_for_active_grants.q1a,
sync_lwcf_annual_inspection_report_for_active_grants.q1b,
sync_lwcf_annual_inspection_report_for_active_grants.q2a,
sync_lwcf_annual_inspection_report_for_active_grants.q2b,
sync_lwcf_annual_inspection_report_for_active_grants.q3a,
sync_lwcf_annual_inspection_report_for_active_grants.q3b,
sync_lwcf_annual_inspection_report_for_active_grants.q3c,
sync_lwcf_annual_inspection_report_for_active_grants.q3d,
sync_lwcf_annual_inspection_report_for_active_grants.q3e,
sync_lwcf_annual_inspection_report_for_active_grants.q3f,
sync_lwcf_annual_inspection_report_for_active_grants.q4,
sync_lwcf_annual_inspection_report_for_active_grants.q5,
sync_lwcf_annual_inspection_report_for_active_grants.q6a,
sync_lwcf_annual_inspection_report_for_active_grants.q6b,
sync_lwcf_annual_inspection_report_for_active_grants.q6c,
sync_lwcf_annual_inspection_report_for_active_grants.attest,
sync_lwcf_annual_inspection_report_for_active_grants.attestdate,
sync_lwcf_annual_inspection_report_for_active_grants.localcontact,
sync_lwcf_annual_inspection_report_for_active_grants.regconname,
sync_lwcf_annual_inspection_report_for_active_grants.regcontitle,
tbl_lwcf.status,
tbl_lwcf.grantee,
fc_nc_counties.name_locas,
fc_nc_counties.rrs_regions
FROM
sync_lwcf_annual_inspection_report_for_active_grants,
fc_nc_counties,
tbl_lwcf,
fc_lwcf_grant_boundaries,
fc_lwcf_grant_points,
fc_lwcf_rrs_regions
WHERE
tbl_lwcf.projectnbr37 = fc_lwcf_grant_points.projectid
AND
fc_lwcf_grant_boundaries.projectid = tbl_lwcf.projectnbr37
AND
fc_lwcf_grant_boundaries.rrs_regions = fc_lwcf_rrs_regions.rrs_regions
AND
fc_nc_counties.rrs_regions = fc_lwcf_rrs_regions.rrs_regions
AND
sync_lwcf_annual_inspection_report_for_active_grants.projectnum = fc_lwcf_grant_points.projectid
【问题讨论】:
-
提示:
JOIN。ON. -
嗯,我要说的第一件事是,尤其是像
sync_lwcf_annual_inspection_report_for_active_grants这样的表名,如果你在表名后写一些字符,它会为该表创建一个别名,然后你可以在其余的查询。如果您说FROM sync_lwcf_annual_inspection_report_for_active_grants r,然后将whatever.sync_lwcf_annual_inspection_report_for_active_grants的所有其他用法替换为r,该查询会变得更好,例如:SELECT r.projectnum, r.projectnumother ... FROM sync_lwcf_annual_inspection_report_for_active_grants r INNER JOIN... -
您的实际问题是什么?你的查询有效吗?如果没有,会发生什么?你得到的行太少了吗?如果有,请说明缺少哪些。还是行太多?可能重复?您使用的连接语法已经过时了近三个世纪。看到你在使用它是令人惊讶的。任何最近(嗯,实际上不到 20 年)的教程或书籍都应该更好地告诉你。
-
@ThorstenKettner 可能不是 3 个 世纪,但我可以坚持几十年! :)
-
@Caius Jard:天哪,错字了。刚看了一部科幻电影;也许这就是原因。 :D 顺便说一句,我喜欢你的回答。非常彻底。
标签: sql postgresql join select