2012-07-23 79 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將帶回兩個表中的所有行,無論它們是否匹配。