'YY'多次指定了'TRANSACTION_EVENT_ID'列

时间:2015-11-03 12:44:49

标签: sql-server sql-server-2008

我写了这样一个查询:

SELECT  
    YY.ACCOUNT_ID,
    YY.TRANSACTION_EVENT_ID 
FROM
   (SELECT * 
    FROM dbo.TRANSACTION TE
    JOIN dbo.FUND_TRANSACTION FT
    ON TE.TRANSACTION_EVENT_ID = FT.TRANSACTION_EVENT_ID
    JOIN ACCOUNT A
    ON FT.ACCOUNT_ID = A.ACCOUNT_ID) AS YY

并得到这样的错误:

  

“YY”

多次指定了“TRANSACTION_EVENT_ID”列

选中此项:SQL The column 'Id' was specified multiple times

但问题仍未解决。我做错了什么?

2 个答案:

答案 0 :(得分:5)

问题在于SELECT *。由于在选择中可以重复相同的列名称。

明确提及列名。以下查询将有效。

SELECT  YY.ACCOUNT_ID, YY.TRANSACTION_EVENT_ID 
FROM (  SELECT FT.ACCOUNT_ID, FT.TRANSACTION_EVENT_ID
        FROM dbo.[TRANSACTION] TE
        JOIN dbo.FUND_TRANSACTION FT ON TE.TRANSACTION_EVENT_ID = FT.TRANSACTION_EVENT_ID
        JOIN ACCOUNT A ON FT.ACCOUNT_ID = A.ACCOUNT_ID
     ) AS YY

答案 1 :(得分:2)

您必须指定要从中选择的表TRANSACTION_EVENT_ID。您必须使用别名/表名而不是*指定列列表。类似的东西:

SELECT  
    YY.ACCOUNT_ID,
    YY.TRANSACTION_EVENT_ID 
FROM
   (SELECT A.ACCOUNT_ID,
           TE.TRANSACTION_EVENT_ID  
    FROM dbo.TRANSACTION TE
    JOIN dbo.FUND_TRANSACTION FT
    ON TE.TRANSACTION_EVENT_ID = FT.TRANSACTION_EVENT_ID
    JOIN ACCOUNT A
    ON FT.ACCOUNT_ID = A.ACCOUNT_ID) AS YY