1

我正在尝试在子查询上构建内部联接。我不断收到一条错误消息,说它无法重新打开表“t1”。

这就是我试图用简单的英语做的事情:

select all instances of "SystemHeader_Name" from "temp2" where "SystemHeader_Name" and "System_Value" are shared across "Tool_Name"

这是我的尝试:

SELECT t1.SystemHeader_Name FROM temp2 t1
INNER JOIN (
    SELECT DISTINCT Tool_Name, SystemHeader_Name, System_Value FROM temp2
) AS t2 ON (t2.SystemHeader_Name = t1.SystemHeader_Name 
    AND t2.System_Value = t1.System_Value);

我该如何做到这一点?

示例

Tool_Name,SystemHeader_Name,System_Value
t1,h1,v1
t1,h2,v2
t2,h1,v1

结果应该是:

h1

问题
经过更多挖掘,我确定我的问题与临时表有关。从这个文件:You cannot refer to a TEMPORARY table more than once in the same query.

看起来我需要想出一个比使用临时表更好的方法。谢谢大家的帮助。

4

2 回答 2

1

这应该这样做:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;

SELECT DISTINCT t1.SystemHeader_Name
FROM temp2 t1
JOIN temp2 t2
    ON t2.SystemHeader_Name = t1.SystemHeader_Name
    AND t2.System_Value = t1.System_Value
    AND t2.Tool_Name <> t1.Tool_Name
于 2013-11-08T17:47:25.493 回答
1

尝试这个:

SELECT distinct t1.SystemHeader_Name
FROM temp2 t1
where exists(
    SELECT 'X'
    FROM temp2 t2
    WHERE t2.system_value = t1.system_value
    AND t2.tool_name <> t1.tool_name
    AND t2.systemheader_name = t1.systemheader_name
)

我使用存在而不是加入,因为您不想要所有行,但如果存在另一个系统标头则需要一个

告诉我是否完成了你的任务。

于 2013-11-08T17:54:56.677 回答