2015-08-13 76 views
0

我有兩個查詢可以工作,但我希望根據第一個查詢的結果完成第二個查詢並將所有結果連接在一起。基於公共數據字段加入兩個查詢的結果

首先查詢是

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結果的組合

+0

您是否使用區分大小寫排序規則?無論哪種方式,我都以爲LIKE忽視了案件。 ?!?!什麼是00000000? – Strawberry

回答

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 
+0

謝謝!我認爲這是行得通的。 – th3louvre

+0

從性能的角度來看,可能會有更好的方法,但沒有沉重的想法,這是你已有的最簡單的擴展。 – xQbert

1

我會告訴你用「with」子句,但MySQL不支持它,但你仍然可以處理你的查詢,如「表」或「意見」。 你可以閱讀和看到這裏例如,如何與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) 
相關問題