這是運行速度非常慢的MS Access查詢。 Results
表是主表,而P
,B
,S
和C
表是子表。列組合Index
和ResultID
在所有這些表中都是獨一無二的。因此,例如,表C
中只能有一行,其中Index
爲「1」,ResultID
爲2.我沒有使用JOIN,因爲MS Access只支持內部連接,而我需要的是外部連接,因爲我即使在任何其他表中沒有對應的記錄,也希望顯示錶Results
中的所有記錄。在MS ACCESS查詢中優化一堆嵌套SELECTs
SELECT
[Results].[Id],
[Results].[Name],
[Results].[OfferPrice],
[Results].[RegPrice],
[Results].[ImageFileName],
[Results].[Image],
[Results].[URL],
(SELECT P.P FROM P WHERE P.ResultID = Results.Id AND P.Index = "0") as P0,
(SELECT P.P FROM P WHERE P.ResultID = Results.Id AND P.Index = "1") as P1,
(SELECT P.P FROM P WHERE P.ResultID = Results.Id AND P.Index = "2") as P2,
(SELECT P.P FROM P WHERE P.ResultID = Results.Id AND P.Index = "3") as P3,
(SELECT P.P FROM P WHERE P.ResultID = Results.Id AND P.Index = "4") as P4,
(SELECT P.P FROM P WHERE P.ResultID = Results.Id AND P.Index = "5") as P5,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "0") as B0,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "1") as B1,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "2") as B2,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "3") as B3,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "4") as B4,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "5") as B5,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "6") as B6,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "7") as B7,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "8") as B8,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "9") as B9,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "10") as B10,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "11") as B11,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "12") as B12,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "13") as B13,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "14") as B14,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "15") as B15,
(SELECT B.B FROM B WHERE B.ResultID = Results.Id AND B.Index = "16") as B16,
(SELECT S.S FROM S WHERE S.ResultID = Results.Id AND S.Index = "1") as S1,
(SELECT S.S FROM S WHERE S.ResultID = Results.Id AND S.Index = "2") as S2,
(SELECT S.S FROM S WHERE S.ResultID = Results.Id AND S.Index = "3") as S3,
(SELECT S.S FROM S WHERE S.ResultID = Results.Id AND S.Index = "4") as S4,
(SELECT S.S FROM S WHERE S.ResultID = Results.Id AND S.Index = "5") as S5,
(SELECT S.S FROM S WHERE S.ResultID = Results.Id AND S.Index = "6") as S6,
(SELECT S.S FROM S WHERE S.ResultID = Results.Id AND S.Index = "7") as S7,
(SELECT S.S FROM S WHERE S.ResultID = Results.Id AND S.Index = "8") as S8,
(SELECT S.S FROM S WHERE S.ResultID = Results.Id AND S.Index = "9") as S9,
(SELECT S.S FROM S WHERE S.ResultID = Results.Id AND S.Index = "10") as S10,
(SELECT C.C FROM C WHERE C.ResultID = Results.Id AND C.Index = "0") as C0,
(SELECT C.C FROM C WHERE C.ResultID = Results.Id AND C.Index = "1") as C1,
(SELECT C.C FROM C WHERE C.ResultID = Results.Id AND C.Index = "2") as C2,
(SELECT C.C FROM C WHERE C.ResultID = Results.Id AND C.Index = "3") as C3,
(SELECT C.C FROM C WHERE C.ResultID = Results.Id AND C.Index = "4") as C4,
(SELECT C.C FROM C WHERE C.ResultID = Results.Id AND C.Index = "5") as C5,
(SELECT C.C FROM C WHERE C.ResultID = Results.Id AND C.Index = "6") as C6,
(SELECT C.C FROM C WHERE C.ResultID = Results.Id AND C.Index = "7") as C7,
(SELECT C.C FROM C WHERE C.ResultID = Results.Id AND C.Index = "8") as C8,
(SELECT C.C FROM C WHERE C.ResultID = Results.Id AND C.Index = "9") as C9,
(SELECT C.C FROM C WHERE C.ResultID = Results.Id AND C.Index = "10") as C10
FROM
Results
什麼讓你相信Access不支持外連接? – Brad 2013-02-12 16:25:29