SQL组合了不同大小的多个表

时间:2015-07-03 00:15:07

标签: sql sql-server

因为它与我的查询一致,因此我生成了4个表。我想在查询中将这些合并为一个。以下是我要做的一般事例

Table_1 (largest)
+----------+----------+----------+----------+----------+
|Col_1     |Col_2     |Col_3     |Col_4     |Col_5     |
+----------+----------+----------+----------+----------+
|  A1      |  B1      |  C1      |  D1      |  E1      |
+----------+----------+----------+----------+----------+
|  A2      |  B2      |  C2      |  D2      |  E1      |
+----------+----------+----------+----------+----------+
|  A3      |  B2      |  C3      |  D3      |  E2      |
+----------+----------+----------+----------+----------+
|  A4      |  B3      |  C4      |  D4      |  E3      |
+----------+----------+----------+----------+----------+

Table_2 (2nd largest)
+-------+-------+-------+-------+
| Col_5 | Col_6 | Col_7 | Col_8 |
+-------+-------+-------+-------+
|  E1   |  F1   |  G1   |  H1   |
+-------+-------+-------+-------+
|  E2   |  F2   |  G2   |  H1   |
+-------+-------+-------+-------+
|  E3   |  F3   |  G3   |  H2   |
+-------+-------+-------+-------+

Table_3 (3rd largest)
+-------+-------+--------+--------+
| Col_8 | Col_9 | Col_10 | Col_11 |
+-------+-------+--------+--------+
|  H1   |  I1   |  J1    |  K1    |
+-------+-------+--------+--------+
|  H2   |  I2   |  J2    |  K1    |
+-------+-------+--------+--------+
|  H3   |  I3   |  J3    |  K2    |
+-------+-------+--------+--------+

Table_4 (smallest)
+--------+--------+--------+
| Col_11 | Col_12 | Col_13 |
+--------+--------+--------+
|   K1   |   L1   |   M1   |
+--------+--------+--------+
|   K2   |   L2   |   M1   |
+--------+--------+--------+

Query Generated Table
+--------+--------+--------+--------+--------+--------+--------+--------+
| Col_1  | Col_3  | Col_5  | Col_6  | Col_8  | Col_9  | Col_11 | Col_12 |
+--------+--------+--------+--------+--------+--------+--------+--------+
|   A1   |   C1   |   E1   |   F1   |   H1   |   I1   |   K1   |   L1   |
+--------+--------+--------+--------+--------+--------+--------+--------+
|   A2   |   C2   |   E1   |   F1   |   H1   |   I1   |   K1   |   L1   |
+--------+--------+--------+--------+--------+--------+--------+--------+
|   A3   |   C3   |   E2   |   F2   |   H1   |   I1   |   K1   |   L1   |
+--------+--------+--------+--------+--------+--------+--------+--------+
|   A4   |   C4   |   E3   |   F3   |   H2   |   I2   |   K1   |   L1   |
+--------+--------+--------+--------+--------+--------+--------+--------+


基本上每个表中都有一列将它链接到下一列。但是,每个表中的行数不同,Table_1最大,Table_4最小。这是我目前在SQL数据库中使用的查询。

Declare @JBank INT
Set @JBank = 26

USE
    [MSI_RemoteDeposit]
SELECT
    IsDeleted, Routing, Account, Serial, Amount, TransactionDate, CreateDate, MICR, BatchID
FROM
    Item
WHERE
    BatchID in(
        SELECT BatchID  FROM Batch  WHERE SiteID in(
            SELECT SiteID   FROM Site   WHERE MerchantID in(
                SELECT MerchantID FROM Merchant WHERE (BankID = @JBank)
                )))

SELECT
    BatchID, Name, SiteID
FROM
    Batch
WHERE
    SiteID in(
        SELECT SiteID   FROM Site   WHERE MerchantID in(
            SELECT MerchantID FROM Merchant WHERE (BankID = @JBank)
            ))

SELECT
    SiteID, IsDeleted, MerchantID
FROM
    Site
WHERE
    MerchantID in(
        SELECT MerchantID FROM Merchant WHERE (BankID = @JBank)
        )

SELECT
    MerchantID, Name, BankID 
FROM
    Merchant
WHERE
    MerchantID in (
            SELECT MerchantID   FROM Site   WHERE MerchantID in(
                SELECT MerchantID FROM Merchant WHERE (BankID = @JBank)
            ))

所以我从4个表中的每个表中选择某些行,并且有一个公共行将一个表连接到下一个表。我感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

取决于您想要的结果:

SELECT a.*,b.*,c.*
FROM Table_1 a
LEFT JOIN Table_2 b ON a.col_5=b.col_5
LEFT JOIN Table_3 c ON b.col_8=c.col_8;

使用一些空字段为您提供所有结果。

SELECT a.*,b.*,c.*
FROM Table_1 a
INNER JOIN Table_2 b ON a.col_5=b.col_5
INNER JOIN Table_3 c ON b.col_8=c.col_8;

使用所有列中的数据(但行数较少)为您提供所有结果

答案 1 :(得分:0)

查看Join operations on MSDN。我认为您可以将所有四个查询简化为以下内容:

Declare @JBank INT
Set @JBank = 26

USE
    [MSI_RemoteDeposit]
SELECT
    i.IsDeleted as ItemIsDeleted, i.Routing, i.Account, i.Serial, i.Amount, i.TransactionDate, i.CreateDate, i.MICR, i.BatchID
    ,b.Name as BatchName,b.SiteID
    ,s.IsDeleted as SiteIsDeleted,s.MerchantID
    ,m.Name as MerchantName,m.BankID
FROM Item i
Join Batch b on b.BatchID=i.BatchID
Join Site s on s.SiteID=b.SiteID
Join Merchant m on m.MerchantID=s.MerchantID
Where m.BankID=@JBankID