2012-07-18 169 views
4

我忽略了一些微不足道的東西。除了嘗試練習連接兩個查詢之外,此查詢不是特別的原因。我得到的錯誤是SQL Server 2008加入兩個查詢

Msg 156, Level 15, State 1, Line 10 
Incorrect syntax near the keyword 'inner'. 
Msg 156, Level 15, State 1, Line 16 
Incorrect syntax near the keyword 'as'. 

並且查詢

select t.countyName, x.countyName 
    from 
    (

    SELECT DISTINCT Patient.patientid, tblStateCounties.countyName, Patient.countyCode 
    FROM   Patient INNER JOIN 
          tblStateCounties ON Patient.stateCode = tblStateCounties.stateCode AND Patient.countyCode = tblStateCounties.countyCode 
    WHERE  (Patient.patientage > 80) 
    ) 
    inner join 
    (
    SELECT DISTINCT Patient.patientid, tblStateCounties.countyName, Patient.countyCode 
    FROM   Patient INNER JOIN 
          tblStateCounties ON Patient.stateCode = tblStateCounties.stateCode AND Patient.countyCode = tblStateCounties.countyCode 
    WHERE  (Patient.patientage < 80) 
    ) as x on t.countyname=x.countyname 
+1

你忘了集T別名在你的第一個內部聯接 – 2012-07-18 01:43:57

回答

6

您忘記使用別名第一subquery

+1

胡說,我知道這是一些愚蠢的。謝謝:) – wootscootinboogie 2012-07-18 01:46:34

+0

不客氣:) – Conan 2012-07-18 01:50:22

3
select t.countyName, x.countyName 
from 
(

    SELECT DISTINCT Patient.patientid, tblStateCounties.countyName, Patient.countyCode 
    FROM   Patient 
    INNER JOIN tblStateCounties ON Patient.stateCode = tblStateCounties.stateCode 
           AND Patient.countyCode = tblStateCounties.countyCode 
     WHERE  (Patient.patientage > 80) 
) rsT 
inner join 
(
     SELECT DISTINCT Patient.patientid, tblStateCounties.countyName, Patient.countyCode 
     FROM   Patient 
     INNER JOIN tblStateCounties ON Patient.stateCode = tblStateCounties.stateCode 
           AND Patient.countyCode = tblStateCounties.countyCode 
     WHERE  (Patient.patientage < 80) 
) rsX on rsT.countyname=rsX.countyname 
+0

不得不改變別名,但它的後工作:) – wootscootinboogie 2012-07-18 01:43:35

+0

@wootscootinboogie對。 – Bert 2012-07-18 01:44:47

0

使用

(
    SELECT DISTINCT Patient.patientid, tblStateCounties.countyName, Patient.countyCode 
    FROM Patient INNER JOIN tblStateCounties 
    ON Patient.stateCode = tblStateCounties.stateCode 
    AND Patient.countyCode = tblStateCounties.countyCode 
    WHERE (Patient.patientage > 80) 
) as t