我有两个查询,我需要找出他们的结果集之间的差异。我的查询如下。
select star_ident,transition_ident,fix_ident,min(sequence_num)
from corept.std_star_leg c
where airport_ident='KLAS' and data_supplier='J'
group by star_ident,transition_ident;
select name,trans
from skyplan_deploy.deploy_stars
where apt='KLAS';
这是我的两个问题。我最初使用左连接但未能得到结果。
select star_ident,transition_ident,fix_ident,min(sequence_num)
from corept.std_star_leg c
left join
(
select name,trans
from skyplan_deploy.deploy_stars
where apt='KLAS' and name != trans
) a
on star_ident=a.name and fix_ident=a.trans
where airport_ident='KLAS' and data_supplier='J' and a.name is null
group by star_ident,transition_ident;
我尝试了上面的查询,但它完全给出了错误的结果集。任何人都可以帮我做这个吗?
谢谢。
答案 0 :(得分:2)
很难在没有实际表格的情况下进行测试,但有一种方法可以做到这一点;
SELECT t1.*
FROM (
select star_ident,transition_ident,fix_ident,min(sequence_num)
from std_star_leg c
where airport_ident='KLAS' and data_supplier='J'
group by star_ident,transition_ident) t1
LEFT JOIN (select name,trans
from deploy_stars
where apt='KLAS') t2
ON t1.star_ident = t2.name AND t1.fix_ident = t2.trans
WHERE t2.trans IS NULL;
也就是说,将您的选择包装到命名的子选择中,并在它们之间执行标准LEFT JOIN
。
这将显示第一个结果集中的行,而不是第二个结果集中的行。
编辑:要查找第二个但不是第一个子选择的行,您只需将其更改为RIGHT JOIN
,选择t2.*
并对t1.fix_ident
进行无效检查; < / p>
SELECT t2.*
FROM (
select star_ident,transition_ident,fix_ident,min(sequence_num)
from std_star_leg c
where airport_ident='KLAS' and data_supplier='J'
group by star_ident,transition_ident) t1
RIGHT JOIN (select name,trans
from deploy_stars
where apt='KLAS') t2
ON t1.star_ident = t2.name AND t1.fix_ident = t2.trans
WHERE t1.fix_ident IS NULL;