2013-08-05 73 views
0

我已經得到了這個查詢,我試圖使用列標識符(我不知道它是如何調用的),但有些似乎工作,其他不是。SQL查詢中的表別名

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標識工作正常,和連接工作正常,但是當我嘗試添加的最後一個與R1和R2標識符JOIN,它不會出現正在工作,我找不到差異。 我有的錯誤是:R1/R2附近的語法不正確。 我試過使用AS,但它也無法正常工作,所以我確定它是一個語法問題還是其他地方的問題的結果。 順便說一句,這是MS SQL。

任何幫助將不勝感激,謝謝。

+0

他們被稱爲*表別名*。請明確提及您得到的錯誤。 – GolezTrol

+0

R1附近的語法不正確 R2附近的語法不正確 – user2618988

回答

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 
+0

我只需要將SELECT *替換爲我需要的列名,否則會出現兩次出現列的錯誤。雖然像魅力一樣工作,非常感謝! – user2618988

+0

你更受歡迎。我很高興我可以幫助:) – peterm

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 '%'