0

我有这样的查询

SELECT DISTINCT
    [fldID],
    [fldValue]
            FROM
                (
                    (
                        SELECT
                            alias1.[fldID],
                            alias1.[fldValue]
                        FROM someT as alias1
                            ....
                    )
                ) AS subQuery_1
            INNER JOIN
            (
                SELECT
                    alias1.[fldID],
                    alias1.[fldValue]
                FROM someT as alias1
                    ...
            ) AS subQuery_2 USING( fldID, fldValue) 

它在 MySQL 中运行良好,但 SQL Server 不支持 USING。所以我必须像这样使用 JOIN :

ON [subQuery_1].[fldID] = [subQuery_2].[fldID] 
AND [subQuery_1].[fldValue] = [subQuery_2].[fldValue]

但是这个查询会导致错误:

Ambiguous column name fldID 
Ambiguous column name fldValue

那么,如何在 SQL Server 中实现这种 JOIN 呢?

4

2 回答 2

2

您只需要指定从哪个子查询中获取这些列(在您加入两列时,您选择哪一个并不重要)

SELECT DISTINCT
subQuery_1.[fldID],
subQuery_1.[fldValue]
        FROM
            (
                (
                    SELECT
                        alias1.[fldID],
                        alias1.[fldValue]
                    FROM someT as alias1
                        ....
                )
            ) AS subQuery_1
        INNER JOIN
        (
            SELECT
                alias1.[fldID],
                alias1.[fldValue]
            FROM someT as alias1
                ...
        ) AS subQuery_2 
 ON [subQuery_1].[fldID] = [subQuery_2].[fldID] 
 AND [subQuery_1].[fldValue] = [subQuery_2].[fldValue]
于 2013-10-10T11:50:43.940 回答
0

在您的查询中,您没有在最终选择中添加表别名。您需要指定来自哪个表的列,如下所示:

SELECT DISTINCT
    subQuery_1.[fldID],
    subQuery_1.[fldValue]
            FROM
                (
                    (
                        SELECT
                            alias1.[fldID],
                            alias1.[fldValue]
                        FROM someT as alias1
                            ....
                    )
                ) AS subQuery_1
            INNER JOIN
            (
                SELECT
                    alias1.[fldID],
                    alias1.[fldValue]
                FROM someT as alias1
                    ...
            ) AS subQuery_2 
            on [subQuery_1].[fldID] = [subQuery_2].[fldID] 
AND [subQuery_1].[fldValue] = [subQuery_2].[fldValue]
于 2013-10-11T07:31:06.390 回答