2012-03-10 150 views
-6

我有想一起捆绑成一个单一查询这些多个SQL查询,让我能避免我的应用程序发送多个请求到数据库(我希望收到所有这些数据在一个单次):如何将多个SQL查询合并为一个?

1) select pin, officeNum, isVeteran from table18 where pin = 123; 

2) select streetAddress, apartmentAddress, cityAddress, stateAddress from table1 where case = (select case from table18 where pin = 123); 

3) select unitAddress, cityAddress, streetAddress, apartmentAddress from table5 where pin = 123; 

4) select unitAddress, cityAddress, streetAddress, apartmentAddress from table55 where seqNum = 0 and rfa = (select rfa from table18 where pin = 123); 

5) select unitAddress, cityAddress, streetAddress, apartmentAddress from table103 where histCode = 0 and case = (select case from table18 where pin = 123); 

6) select phone, email from table715 where histSeqNum in (select max(histSeqNum from table715)) 
     and histCode in (select max(histCode) from table715) 
     and case = (select case from table18 where pin = 123); 

这里是我的架构:

(请原谅糟糕的设计,它是从数据库中创建20年前,没有外键)

-Table18(引脚(PK)的情况下, officeNum,isVeteran)

-Table1(情况(PK),caseOfficer,的StreetAddress,apartmentAddress,cityAddress,stateAddress)

-Table5(销(PK),的StreetAddress,apartmentAddress,cityAddress,stateAddress)

-Table55(RFA(CompositeKey ),SEQNUM(CompositeKey),rfaAddress,的StreetAddress,apartmentAddress,cityAddress,stateAddress)

-Table103(情况(CompositeKey),histCode(CompositeKey))

-Table715(情况(CompositeKey),histSeqNum(CompositeKey) ,histCode(CompositeKey),电话,电子邮件)

+2

你准确的问题是什么? – 2012-03-10 20:56:44

+0

@juergend。这是一些其他问题...... – gdoron 2012-03-10 20:58:31

+0

没有看到一个模式有点困难,但它看起来像我使用子查询来执行连接。如果您可以提供更多信息,则应该可以重写,而不必有人需要首先对您的模式进行逆向工程。 – 2012-03-10 20:58:49

回答

2

这里有一组可以联合在一起...... (3,4,5)

select unitAddress, cityAddress, streetAddress, apartmentAddress 
from table5 where pin = 123 
union 
select unitAddress, cityAddress, streetAddress, apartmentAddress 
from table55 where seqNum = 0 and rfa = (select rfa from table18 where pin = 123) 
union 
select unitAddress, cityAddress, streetAddress, apartmentAddress 
from table103 where histCode = 0 and case = (select case from table18 where pin = 123); 

你可以把(2)上也有,如果你不介意这些空白状态.. 。

例如,在2写一样的东西:

select null unitAddress, streetAddress, apartmentAddress, cityAddress, stateAddress 
from table1 
where case = (select case from table18 where pin = 123); 
union 
select unitAddress, cityAddress, streetAddress, apartmentAddress, null 
from table5 where pin = 123 
union 
select unitAddress, cityAddress, streetAddress, apartmentAddress, null 
from table55 where seqNum = 0 and rfa = (select rfa from table18 where pin = 123) 
union 
select unitAddress, cityAddress, streetAddress, apartmentAddress, null 
from table103 where histCode = 0 and case = (select case from table18 where pin = 123); 

还考虑重组你对加入嵌套的选择 - 与此类似:(我敢打赌,优化这些查询会表现不同ence你正在寻找)

select streetAddress, apartmentAddress, cityAddress, stateAddress 
from table1 t1, table18 t18 
where t1.case = t18.case 
and t18.123; 

然后确保t18在引脚上有一个索引,和t1有一个案件的索引。

3

你不能。所有查询都返回固定数量的列。如果您仍然坚持以这种方式检索您的列,请确保所有查询的列数据类型相同,并将它们与工会组合在一起。我不推荐,但它是可能的。

+0

我不明白为什么它不能完成 - 所有人之间显然有可能的联接的表格。 – 2012-03-11 01:52:43

+0

@DavidFaber现在有,没有更早。 – 2012-03-11 07:00:48