8

如何组合output两个或多个SELECT语句,我有多个表,其中包含我需要的一些数据,fetch因此我编写了多个SELECT查询。现在我想组合查询的结果,那么我需要做什么?我希望输出为:

t1.qty,t2.qty,t3.qty 
4

3 回答 3

21

一种选择是:

SELECT (SELECT SUM(qty) FROM Table1 WHERE ...),
       (SELECT SUM(qty) FROM Table2 WHERE ...),
       (SELECT SUM(qty) FROM Table3 WHERE ...)

如果有链接,另一个将加入:

SELECT * 
FROM   (SELECT ID,SUM(qty) FROM Table1 GROUP BY ID) T1
       JOIN (SELECT ID,SUM(qty) FROM Table2 GROUP BY ID) T2
           ON T1.ID = T2.ID
       JOIN (SELECT ID,SUM(qty) FROM Table3 GROUP BY ID) T3
           ON T1.ID = T3.ID

上述选项将在一行中显示结果。

您可能需要联合来组合行:

SELECT qty FROM Table1
UNION
SELECT qty FROM Table2
UNION
SELECT qty FROM Table3

如果您定义更具体的需求,则会有更多选择

于 2013-08-13T08:04:36.150 回答
3

为什么不创建一个一次性获取它们的语句呢?

SELECT tableA.data1, tableB.data2 FROM tableA, tableB WHERE <condition here>
于 2013-08-13T08:04:08.333 回答
0

task1 = "select top" & strmcount &" * from nregistration where " & strtotal1
task2 = "select top" & strfcount & " * from nregistration where " & strtotal2

并行执行

于 2017-01-27T23:43:12.883 回答