我有这个查询,我正在尝试使用列标识符(我不知道它是如何调用的),但有些似乎正在工作,而其他没有。
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。
非常感谢任何帮助,谢谢。
答案 0 :(得分: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
答案 1 :(得分: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 '%'