我正在尝试在子查询上构造内连接。我一直收到错误,说它无法重新打开表“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
问题
经过一番挖掘,我确定我的问题是临时表。来自this文档:You cannot refer to a TEMPORARY table more than once in the same query.
看起来我需要提出比使用临时表更好的方法。谢谢大家的帮助。
答案 0 :(得分: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
答案 1 :(得分: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
)
我使用exists而不是join,因为如果存在另一个systemheader
,则不希望所有行只有一行告诉我是否完成了你的任务。