9

我正在尝试执行此连接操作。由于我是 sql 新手,我发现理解语法和内容时遇到问题。

您认为以下查询有什么问题:

select top 1 * 
from 
    (select * 
     from dbo.transaction_unrated 
        where transaction_date >= '2012/05/01' 
            and transaction_date < '2012/06/01' 
            and content_provider_code_id in (1)
    )   FULL OUTER JOIN 
    (select * 
     from dbo.transaction_rated 
        where transaction_date >= '2012/05/01' 
            and transaction_date < '2012/06/01' 
            and entity_id in (1) 
            and mapping_entity_id = 1)
    ) 
    ON dbo.transaction_unrated.cst_id = dbo.transaction_rated.unrated_transaction_id
4

1 回答 1

12

您需要给派生表起别名。

select top 1 * 
from 
(
     select * 
     from dbo.transaction_unrated 
        where transaction_date >= '2012/05/01' 
            and transaction_date < '2012/06/01' 
            and content_provider_code_id in (1)
) rsQuery1  
FULL OUTER JOIN 
(
     select * 
     from dbo.transaction_rated 
        where transaction_date >= '2012/05/01' 
            and transaction_date < '2012/06/01' 
            and entity_id in (1) 
            and mapping_entity_id = 1)
) rsQuery2 ON rsQuery1.cst_id = rsQuery2.unrated_transaction_id

FULL OUTER JOIN也很不寻常(根据我的经验)。你确定那是你想要的吗?通常,您将执行一个INNER JOINwhich 会在两个表中带回与您的条件匹配的行,或者您将让一个表作为驱动程序并执行LEFTorRIGHT OUTER JOIN将带回驱动表中的所有行,无论是否存在匹配另一张桌子。AFULL OUTER JOIN将带回两个表中的所有行,无论它们是否匹配。

于 2012-07-23T21:00:53.927 回答