1 つのクエリの結果から 2 つの列を使用して、別のテーブルの値を検索するにはどうすればよいですか?
UserID などの単一の列を使用してユーザーの名と姓を検索する「辞書」の問題の例をいくつか見てきましたが、一致ごとに複数の可能な結果と一致する必要がある 2 つの列があります。
現在、出荷準備完了が TRUE であるすべてのレコードを照会し、コードを使用して結果をループ処理し、各製品/色の組み合わせの出荷ラベル ファイルを検索しています。私の目標は、単一のクエリで目的の結果を取得することです。
次の SQL を試しましたが、遅すぎます (数分)。JOIN やその他のトリックを使用すると、これがより速く (1 秒未満) になるのではないかと考えていました。現在、私のコードには約2秒かかります。私が使用している実世界のテーブルは数千レコードの長さで、数百の結果を返します。
Select
tblA.BoxID,
tblA.Product,
tblA.Color,
tblA.Ready_to_Ship AS Ready,
tblB.Shipping_Label_File
From
Table_A tblA,
Table_B tblB
Where
tblB.Product = tblA.Product AND
tblB.Color = tblA.Color AND
tblA.Ready_to_Ship = 'TRUE'
Desired results:
BoxID Product Color Ready Shipping_Label_File
B5255 34xBty2001 Red TRUE ShipLBL-01r_A.txt
B5255 34xBty2001 Red TRUE ShipLBL-01r_B.txt
J6632 34xBty2002 Blue TRUE ShipLBL-07b_D.txt
E2748 34xBty2002 Red TRUE ShipLBL-07r_D.txt
E4716 64d_Dty2005 Red TRUE ShipLBL-05r_B.txt
E4716 64d_Dty2005 Red TRUE ShipLBL-05r_C.txt
Table_A
BoxID Product Color Ready_to_Ship
B5255 34xBty2001 Red TRUE
J6632 34xBty2002 Blue TRUE
F8975 64b_Dty2005 Blue FALSE
F9768 64b_Dty2005 Blue FALSE
I1053 34xBty2001 Green FALSE
J2202 34xBty2001 Blue FALSE
D2986 64a_Dty2005 Blue FALSE
A6210 64b_Dty2005 Blue FALSE
I1088 34xBty2002 Blue FALSE
E2748 34xBty2002 Red TRUE
D7945 64b_Dty2005 Blue FALSE
E4716 64d_Dty2005 Red TRUE
Table_B
Product Color Shipping_Label_File
34xBty2001 Red ShipLBL-01r_A.txt
34xBty2001 Red ShipLBL-01r_B.txt
34xBty2001 Blue ShipLBL-01b_A.txt
34xBty2001 Green ShipLBL-01g_A.txt
34xBty2001 Green ShipLBL-01g_C.txt
34xBty2002 Red ShipLBL-07r_D.txt
34xBty2002 Blue ShipLBL-07b_D.txt
34xBty2002 Green ShipLBL-07g_M.txt
64a_Dty2005 Blue ShipLBL-A3b_A.txt
64a_Dty2005 Green ShipLBL-A3g_E.txt
64b_Dty2005 Red ShipLBL-05r_B.txt
64b_Dty2005 Red ShipLBL-05r_C.txt
64b_Dty2005 Green ShipLBL-05g_A.txt