2013-01-31 66 views
1

我正在努力與Oracle查詢。我想向內部查詢發送一個值,但我沒有成功。我的查詢看起來如下:Oracle參數到內部查詢

SELECT * FROM Pro u 
LEFT JOIN (SELECT * FROM PROLOG d 
WHERE d.Id = (SELECT MAX(Id) FROM PROLOG t 
      WHERE t.Project = **u.Id** 
      AND t.Prodstatus IN (5,40)))z ON (u.ID = z.Project) 
WHERE u.Id = 22; 

我想用22的值22來自臨ü表更換u.Id。請給我一些提示。

回答

0

你並不需要通過該值的子查詢從Pro表,您可以使用prolog表值

SELECT * FROM Pro u 
LEFT JOIN (SELECT * FROM PROLOG d 
WHERE d.Id = (SELECT MAX(Id) FROM PROLOG t 
      WHERE t.Project = d.Project 
      AND t.Prodstatus IN (5,40)))z ON (u.ID = z.Project) 
WHERE u.Id = 22; 
1

我不相信你可以關聯一個連接到一個內嵌視圖,但是你可以將SQL語句簡化爲具有相關謂詞的簡單外連接:

SELECT * 
FROM pro u 
LEFT OUTER JOIN prolog d 
    ON d.project = u.id 
    AND d.Id = (SELECT MAX(Id) 
        FROM prolog t 
        WHERE t.project = u.id 
        AND t.prodstatus IN (5,40)) 
WHERE u.id = 22;