簡體   English   中英

MySQL內部聯接子查詢

[英]MySQL inner join on subquery

我正在嘗試在子查詢上構造內部聯接。 我不斷收到錯誤消息,說它無法重新打開表“ 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.

看起來我需要比使用臨時表提供更好的方法。 謝謝大家的幫助。

應該這樣做:

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

嘗試這個:

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
)

我使用exist而不是join,因為您不希望所有行,但如果一個行存在另一個systemheader

告訴我是否完成任務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM