基于公共数据字段连接两个查询的结果

时间:2015-08-13 16:08:17

标签: mysql sql join

我有两个有效的查询,但我想根据第一个查询的结果完成第二个查询,并将所有结果连接在一起。

第一个查询是

select c.contribution_amount, c.contribution_dt,
c.contribution_descr,c.report_info_id,
p.name_last,p.name_first,p.name_organization,
p.employer,p.occupation,p.job_title,
a.city, a.state_cd, a.postal_code,
r.report_info_ident, r.filer_info_id
from contribution_info c
inner join contribution_persent cp on  c.contribution_info_id=cp.contribution_info_id
inner join persent_info p on cp.persent_info_id=p.persent_info_id
inner join persent_address pa on p.persent_info_id=pa.persent_info_id
inner join address_info a on pa.address_info_id=a.address_info_id
inner join report_info r on c.report_info_id=r.report_info_id
where lower(p.name_last) like '%somebody%' or lower(p.name_organization) like '%somebody%'

,第二个查询是

select f1.filer_ident, fp1.filer_persent_kind_cd,
p1.name_last, p1.name_first,p1.name_organization, p1.persent_type_cd
from filer_info f1 
inner join filer_persent fp1 on f1.filer_info_id=fp1.filer_info_id
inner join persent_info p1 on fp1.persent_info_id=p1.persent_info_id
where f1.filer_info_id=000000000
and fp1.filer_persent_kind_cd='FILER'

第二个查询的00000000需要等于第一个查询的c.report_info_id(也表示为r.report_info_id)

并且行结果需要是所有r,p,c,a,r1,fp1结果的组合

2 个答案:

答案 0 :(得分:1)

在将两个结果集合在一起之后,对我来说似乎是...... 这可以通过将两个查询别名为内联视图,然后根据您的问题语句加入必要的条件...... on A.report_info_ID = B.filer_info_id 来实现...

Select * 
from (
select c.contribution_amount, c.contribution_dt,
c.contribution_descr,c.report_info_id,
p.name_last,p.name_first,p.name_organization,
p.employer,p.occupation,p.job_title,
a.city, a.state_cd, a.postal_code,
r.report_info_ident, r.filer_info_id
from contribution_info c
inner join contribution_persent cp on  c.contribution_info_id=cp.contribution_info_id
inner join persent_info p on cp.persent_info_id=p.persent_info_id
inner join persent_address pa on p.persent_info_id=pa.persent_info_id
inner join address_info a on pa.address_info_id=a.address_info_id
inner join report_info r on c.report_info_id=r.report_info_id
where lower(p.name_last) like '%somebody%' or lower(p.name_organization) like '%somebody%') A
INNER JOIN (
select f1.filer_ident, fp1.filer_persent_kind_cd,
p1.name_last, p1.name_first,p1.name_organization, p1.persent_type_cd
from filer_info f1 
inner join filer_persent fp1 on f1.filer_info_id=fp1.filer_info_id
inner join persent_info p1 on fp1.persent_info_id=p1.persent_info_id
where and fp1.filer_persent_kind_cd='FILER') B
 on A.report_info_ID = B.filer_info_id

答案 1 :(得分:1)

我会告诉你使用“WITH”子句,但MySQL不支持它,但你仍然可以像“TABLES”或“VIEWS”那样处理你的查询。 您可以在这里阅读和查看示例,如何使用MySQL: MySQL "WITH" clause

在你的情况下,它会是这样的:

select * from 
(select c.contribution_amount, c.contribution_dt,
c.contribution_descr,c.report_info_id,
p.name_last,p.name_first,p.name_organization,
p.employer,p.occupation,p.job_title,
a.city, a.state_cd, a.postal_code,
r.report_info_ident, r.filer_info_id
from contribution_info c
inner join contribution_persent cp on     c.contribution_info_id=cp.contribution_info_id
inner join persent_info p on cp.persent_info_id=p.persent_info_id
inner join persent_address pa on p.persent_info_id=pa.persent_info_id
inner join address_info a on pa.address_info_id=a.address_info_id
inner join report_info r on c.report_info_id=r.report_info_id
where lower(p.name_last) like '%somebody%' or lower(p.name_organization) like '%somebody%') a
inner join
(select f1.filer_ident, fp1.filer_persent_kind_cd,
p1.name_last, p1.name_first,p1.name_organization, p1.persent_type_cd
from filer_info f1 
inner join filer_persent fp1 on f1.filer_info_id=fp1.filer_info_id
inner join persent_info p1 on fp1.persent_info_id=p1.persent_info_id
where fp1.filer_persent_kind_cd='FILER') b
on b.filer_info_id = a.report_info_id 
where b.filer_persent_kind_cd='FILER'

我不确定你是否只想要c.report_info_id的第一个值,但如果你是,你可以很容易地将这个条件用于WHERE子句:

and a.report_info_id in (SELECT TOP 1 report_info_id from contribution_info)