4

我在这里忽略了一些微不足道的事情。除了尝试练习连接两个查询之外,此查询没有任何特殊原因。我得到的错误是

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
4

3 回答 3

6

您忘记在第一个中使用别名subquery

于 2012-07-18T01:41:11.947 回答
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
于 2012-07-18T01:41:27.703 回答
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
于 2012-07-18T03:07:12.227 回答