连接表的多个条件

时间:2014-10-04 03:06:39

标签: sql inner-join

如果INNER JOIN可以使用单个表但条件不同吗? 我想知道是否可以将以下查询合并为一个:

UPDATE st
SET st.GLAccount = gt.GLAccount
FROM StockTransactions st
INNER JOIN GLTransactions gt ON gt.TransactionID = st.PurchaseTransactionID 
                                AND gt.Quantity IS NOT NULL AND st.Item = gt.Item
Go


UPDATE st
SET st.GLAccount = gt.GLAccount
FROM StockTransactions st
INNER JOIN GLTransactions gt ON gt.TransactionID = st.ID AND st.Item = gt.Item
                                AND (st.Type = 160 OR st.Type = 165)
GO              


UPDATE st
SET st.GLAccount = gt.GLAccount
FROM StockTransactions st
INNER JOIN GLTransactions gt ON gt.TransactionID = st.ID AND st.Item = gt.Item
                                AND gt.LineType = 1
GO  

1 个答案:

答案 0 :(得分:2)

怎么样:

UPDATE  st
SET     st.GLAccount = gt.GLAccount
FROM    StockTransactions st
        INNER JOIN GLTransactions gt ON ( gt.TransactionID = st.PurchaseTransactionID
                                          AND gt.Quantity IS NOT NULL
                                          AND st.Item = gt.Item
                                        )
                                        OR ( gt.TransactionID = st.ID
                                             AND st.Item = gt.Item
                                             AND ( ( st.Type = 160
                                                     OR st.Type = 165
                                                   )
                                                   OR gt.LineType = 1
                                                 )
                                           )