0

我有这个查询,我正在尝试使用列标识符(我不知道它是如何调用的),但有些似乎正在工作,而另一些则没有。

SELECT R1.EC_Date, R1.JO_Num, R1.EC_Intitule, R1.EC_Sens, R1.EC_RefPiece, R1.EC_Piece, R1.CG_Num, R1.EC_Reference, R1.JM_Date, R1.EC_Jour, R2.EC_Lettrage, R2.EC_Pointage
FROM ((F_COMPTEA CA INNER JOIN F_ECRITUREA EA ON CA.CA_Num = EA.CA_Num)
               INNER JOIN F_ECRITUREC EC ON EA.EC_No = EC.EC_No) R1
        INNER JOIN (
            SELECT * 
            FROM F_ECRITUREC
            WHERE (CG_Num LIKE '401%' OR CG_Num LIKE '411%')
            AND (JO_Num = 'RAN' OR JO_Num = 'ACH' OR JO_Num = 'VTE')
            AND EC_RefPiece IN (
                SELECT EC_RefPiece
                FROM F_ECRITUREA EA INNER JOIN F_ECRITUREC EC ON EA.EC_No = EC.EC_No
                WHERE EC_Jour BETWEEN '1' AND '31'
                AND JM_Date = '2013-01-07'
                AND JO_Num = 'ACH'
                AND EA.CA_Num LIKE '%')) R2
        ON R1.EC_RefPiece = R2.EC_RefPiece
WHERE EC_Jour BETWEEN '1' AND '31'
AND JM_Date = '2013-01-07'
AND JO_Num = 'ACH'
AND EA.CA_Num LIKE '%';

在这种情况下,CA、EA 和 EC 标识符工作正常,并且 JOIN 工作正常,但是当我尝试使用 R1 和 R2 标识符添加最后一个 JOIN 时,它似乎没有工作,我可以'找不到区别。我遇到的错误是:R1/R2 附近的语法不正确。我尝试使用 AS,但它也不起作用,所以我确定这是语法问题,还是其他地方问题的结果。顺便说一句,它是 MS SQL。

任何帮助将不胜感激,谢谢。

4

2 回答 2

1

SELECT子查询缺少子句R1。而且您可能不再需要WHERE外部选择中的条件。您将它们应用到R2. 但可以肯定的是,您不再可以EA在外部选择中引用列。您的外部查询中只有 R1 和 R2 别名可用。

话虽如此,我相信您的查询应该是这样的

SELECT R1.EC_Date, 
       R1.JO_Num, 
       R1.EC_Intitule, 
       R1.EC_Sens, 
       R1.EC_RefPiece, 
       R1.EC_Piece, 
       R1.CG_Num, 
       R1.EC_Reference, 
       R1.JM_Date, 
       R1.EC_Jour, 
       R2.EC_Lettrage, 
       R2.EC_Pointage
  FROM 
(
    SELECT * -- you has been missing SELECT here
      FROM F_COMPTEA CA JOIN F_ECRITUREA EA 
        ON CA.CA_Num = EA.CA_Num JOIN F_ECRITUREC EC 
        ON EA.EC_No = EC.EC_No
) R1 JOIN 
(
    SELECT * 
      FROM F_ECRITUREC
     WHERE (CG_Num LIKE '401%' OR CG_Num LIKE '411%')
       AND (JO_Num = 'RAN' OR JO_Num = 'ACH' OR JO_Num = 'VTE')
       AND EC_RefPiece IN 
       (
            SELECT EC_RefPiece
              FROM F_ECRITUREA EA INNER JOIN F_ECRITUREC EC ON EA.EC_No = EC.EC_No
             WHERE EC_Jour BETWEEN '1' AND '31'
               AND JM_Date = '2013-01-07'
               AND JO_Num = 'ACH'
               AND EA.CA_Num LIKE '%'
        )
) R2 ON R1.EC_RefPiece = R2.EC_RefPiece
-- WHERE EC_Jour BETWEEN '1' AND '31'
--   AND JM_Date = '2013-01-07'
--   AND JO_Num = 'ACH'
--   AND EA.CA_Num LIKE '%' you're no longer can reference `EA` columns in outer select. Only R1 and R2 aliases available in your outer query
于 2013-08-05T08:50:34.430 回答
0
SELECT
R1.EC_Date,
R1.JO_Num,
R1.EC_Intitule,
R1.EC_Sens,
R1.EC_RefPiece,
R1.EC_Piece,
R1.CG_Num,
R1.EC_Reference,
R1.JM_Date,
R1.EC_Jour,

R2.EC_Lettrage,
R2.EC_Pointage

FROM

(SELECT *
FROM
F_COMPTEA 
INNER JOIN F_ECRITUREA EA
ON CA.CA_Num = EA.CA_Num
INNER JOIN F_ECRITUREC EC
ON EA.EC_No = EC.EC_No
) AS R1


INNER JOIN
(
SELECT * 
FROM
F_ECRITUREC
WHERE
(CG_Num LIKE '401%' OR CG_Num LIKE '411%')
AND (JO_Num = 'RAN' OR JO_Num = 'ACH' OR JO_Num = 'VTE')
AND EC_RefPiece IN (SELECT EC_RefPiece FROM F_ECRITUREA EA INNER JOIN F_ECRITUREC EC ON EA.EC_No = EC.EC_No
WHERE EC_Jour BETWEEN '1' AND '31'
AND JM_Date = '2013-01-07'
AND JO_Num = 'ACH'
AND EA.CA_Num LIKE '%')

) AS R2

ON R1.EC_RefPiece = R2.EC_RefPiece

WHERE 
R2.EC_Jour BETWEEN '1' AND '31'
AND R2.JM_Date = '2013-01-07'
AND R2.JO_Num = 'ACH'
--THERE IS NO CONDITION WITHIN THIS LIKE?
AND R2.CA_Num LIKE '%'
于 2013-08-05T08:58:23.733 回答