如何从postgresql中的UNION的第一个表中获取列?
我有一个需要获取所有由created_by“[email protected]”或shared_to“[email protected]”创建的文件。以下是实现结果所需的2个表格。如何从postgresql中的UNION的第一个表中获取列?
filter_analysis_store
fa_identifier,created_by,case_no
FA000179,[email protected],01998455
FA000183,[email protected],01998455
FA000184,[email protected],02039960
FA000185,[email protected],02039960
FA000187,[email protected],02039596
FA000189,[email protected],02039960
FA000190,[email protected],02029418
FA000191,[email protected],02029418
FA000192,[email protected],02039596
FA000193,[email protected],02039596
FA000194,[email protected],02039596
FA000195,[email protected],01912596
FA000199,[email protected],02039596
share_analysis_store
fa_identifier,shared_by,shared_to
FA000173,[email protected],[email protected]
FA000196,[email protected],[email protected]
FA000180,[email protected],[email protected]
FA000198,[email protected],[email protected]
我用“联盟”拿出一个查询这给我的结果,但我不得不放弃了第二次查询空字符串。如何获得各自的case_no缺少的?上面的查询
FA000184 02039960
FA000183 01998455
FA000193 02039596
FA000199 02039596
FA000180
FA000189 02039960
FA000195 01912596
FA000185 02039960
FA000194 02039596
FA000190 02029418
FA000191 02029418
FA000173
FA000192 02039596
FA000198
FA000179 01998455
FA000187 02039596
FA000196
你并不需要一个UNION
产生行,由或创建共享“一个@ a.com“,简单的LEFT JOIN
就足够了(LEFT
,因为fa_identifier
可能永远不会共享)。
select distinct on (fa_identifier) fa_identifier, f.case_no
from filter_analysis_store f
left join share_analysis_store s using (fa_identifier)
where f.created_by = '[email protected]'
or s.shared_to = '[email protected]'
distinct on
,须出示每fa_identifier
只有一次。如果这是filter_analysis_store
中的主键(或者至少有一个唯一键),那么select distinct fa_identifier, f.case_no
就足够了。
您可以帮助我将此结果与另一个[此处的问题]结合使用(http:// stackoverflow .COM /问题/ 42529403 /如何配搭,一列对结果的查询,结果到一个表列值和-GET-M)?需要列出每个项目下的所有情况编号 – Ricky
的
select fas.fa_identifier, fas.case_no as case_no from filter_analysis_store fas
where created_by like '[email protected]' UNION
select sas.fa_identifier, '' from share_analysis_store sas
where shared_to like '[email protected]';
输出试试这个
SELECT fas.fa_identifier,
fas.case_no
FROM filter_analysis_store fas
RIGHT OUTER JOIN
(SELECT fas.fa_identifier
FROM filter_analysis_store fas
WHERE created_by LIKE '[email protected]'
UNION
SELECT sas.fa_identifier
FROM share_analysis_store sas
WHERE shared_to LIKE '[email protected]'
) AS lol
ON lol.fa_identifier = fas.fa_identifier;
从哪里可以想象这些缺失值将会出现? –
@TimBiegeleisen:从'filter_analysis_store'第一张表。我只提到了两个表,可以用来解决这个问题 – Ricky
我相信OP想要结合第一个表和两个连接? –