2012-07-23 77 views
4

我正在尝试执行此联接操作。由于我是sql新手,我发现问题理解的语法和东西。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 
+0

什么是错误? – 2012-07-23 20:59:37

+0

Msg 156,Level 15,State 1,Line 9 关键字'FULL'附近的语法不正确。 Msg 170,Level 15,State 1,Line 16 Line 16:'''附近语法不正确。 – mariner 2012-07-23 21:00:40

+1

你想达到什么目的? – DeanOC 2012-07-23 21:00:49

回答

8

你需要你的别名派生表。

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 JOIN,这会在两个表格中返回与您的条件相匹配的行,或者您将让一个表成为驱动程序并执行LEFTRIGHT OUTER JOIN,这将返回驱动表中的所有行,无论是否存在另一个表中的匹配。 A FULL OUTER JOIN将带回两个表中的所有行,无论它们是否匹配。