2013-03-31 34 views
0

我有這個疑問:where子句中的MySQL min函數,它是如何工作的?

select 
    s_acctbal, s_name, n_name, p_partkey, p_mfgr, 
    s_address, s_phone, s_comment 
from 
    part, supplier, partsupp, nation, region 
where 
    p_partkey = ps_partkey 
    and s_suppkey = ps_suppkey 
    and p_size = 15 
    and p_type like '%BRASS' 
    and s_nationkey = n_nationkey 
    and n_regionkey = r_regionkey 
    and r_name = 'EUROPE' 
    and ps_supplycost = (
     select 
      min(ps_supplycost) 
     from 
      partsupp, supplier, nation, region 
     where 
      p_partkey = ps_partkey 
      and s_suppkey = ps_suppkey 
      and s_nationkey = n_nationkey 
      and n_regionkey = r_regionkey 
      and r_name = 'EUROPE' 
    ) 
order by 
    s_acctbal desc, n_name, s_name, p_partkey; 

首先的疑問:

爲什麼在子查詢(內選)r_name='EUROPE'再次設置?我知道行p_partkey = ps_partkey給這個子選擇外部選擇相同的條件,所以沒有必要再次設置它,或不?

二疑問:

在內部選擇製作一些個體差異(即我看不到,但我想知道)查詢中的行r_name = 'EUROPE'。如果我計算原始查詢的結果,我有460個函數。如果我走出r_name = 'EUROPE'的內部選擇,我只有144個委託。 這看起來很瘋狂,有了這個條件(因此有更多的限制),並且比我離開條件時有更多的結果。爲什麼會發生?

FYI(也許這幫助爲什麼我的心與此查詢吹):

原來的查詢返回:

s_acctbal s_name    n_name   p_partkey p_mfgr   s_address        s_phone   s_comment                      
--------- ------------------ -------------- --------- -------------- -------------------------------------- --------------- ---------------------------------------------------------------------------------------------- 
9938.53 Supplier#000005359 UNITED KINGDOM 185358 Manufacturer#4 QKuHYh,vZGiwu2FWEJoLDx04    33-429-790-6131 blithely silent pinto beans are furiously. slyly final deposits acros 

9937.84 Supplier#000005969 ROMANIA  108438 Manufacturer#1 ANDENSOSmk,miq23Xfb5RWt6dvUcvt6Qa  29-520-692-3537 carefully slow deposits use furiously. slyly ironic platelets above the ironic 

9936.22 Supplier#000005250 UNITED KINGDOM 249  Manufacturer#4 B3rqp0xbSEim4Mpy2RH J     33-320-228-2957 blithely special packages are. stealthily express deposits across the closely final instructi 

9923.77 Supplier#000002324 GERMANY  29821  Manufacturer#4 y3OD9UywSTOk       17-779-299-1839 quickly express packages breach quiet pinto beans. requ 

9871.22 Supplier#000006373 GERMANY  43868  Manufacturer#5 J8fcXWsTqM        17-813-485-8637 never silent deposits integrate furiously blit 

9870.78 Supplier#000001286 GERMANY  81285  Manufacturer#2 YKA,E2fjiVd7eUrzp2Ef8j1QxGo2DFnosaTEH 17-516-924-4574 final theodolites cajole slyly special, 

9870.78 Supplier#000001286 GERMANY  181285 Manufacturer#4 YKA,E2fjiVd7eUrzp2Ef8j1QxGo2DFnosaTEH 17-516-924-4574 final theodolites cajole slyly special, 

9852.52 Supplier#000008973 RUSSIA   18972  Manufacturer#2 t5L67YdBYYH6o,Vz24jpDyQ9    32-188-594-7038 quickly regular instructions wake-- carefully unusual braids into the expres 

9847.83 Supplier#000008097 RUSSIA   130557 Manufacturer#2 xMe97bpE69NzdwLoX      32-375-640-3593 slyly regular dependencies sleep slyly furiously express dep 

9847.57 Supplier#000006345 FRANCE   86344  Manufacturer#1 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques 

9847.57 Supplier#000006345 FRANCE   173827 Manufacturer#2 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques 

無內選擇條件的查詢r_name = 'EUROPA'回報:

s_acctbal s_name    n_name   p_partkey p_mfgr   s_address        s_phone   s_comment                    
--------- ------------------ -------------- --------- -------------- --------------------------------------- --------------- ------------------------------------------------------------------------------------- 
9938.53 Supplier#000005359 UNITED KINGDOM 185358 Manufacturer#4 QKuHYh,vZGiwu2FWEJoLDx04    33-429-790-6131 blithely silent pinto beans are furiously. slyly final deposits acros 

9817.10 Supplier#000002352 RUSSIA   124815 Manufacturer#2 4LfoHUZjgjEbAKw TgdKcgOc4D4uCYw   32-551-831-1437 blithely pending packages across the ironic accounts grow slyly after the furiously 

9721.95 Supplier#000008757 UNITED KINGDOM 156241 Manufacturer#3 Atg6GnM4dT2        33-821-407-2995 ironic, even dolphins above the furiously ironic foxes sleep slyly around the caref 

9643.55 Supplier#000005148 ROMANIA  107617 Manufacturer#1 kT4ciVFslx9z4s79p Js825     29-252-617-4850 doggedly even ideas boost furiously against the furiously express 

9612.94 Supplier#000003228 ROMANIA  198189 Manufacturer#4 KDdpNKN3cWu7ZSrbdqp7AfSLxx,qWB   29-325-784-8187 carefully pending accounts serve. furiously close deposits boost slyly. q 

9571.83 Supplier#000004305 ROMANIA  179270 Manufacturer#2 qNHZ7WmCzygwMPRDO9Ps     29-973-481-1831 furiously final deposits 

9558.10 Supplier#000003532 UNITED KINGDOM 88515  Manufacturer#4 EOeuiiOn21OVpTlGguufFDFsbN1p0lhpxHp  33-152-301-2164 daring, sly accounts breach about th 

9280.27 Supplier#000007194 ROMANIA  47193  Manufacturer#3 zhRUQkBSrFYxIAXTfInj vyGRQjeK   29-318-454-2133 slyly ironic requests despite the unusual ins 

9249.35 Supplier#000003973 FRANCE   26466  Manufacturer#1 d18GiDsL6Wm2IsGXM,RZf1jCsgZAOjNYVThTRP4 16-722-866-1658 quickly ironic sauternes use b 

9249.35 Supplier#000003973 FRANCE   33972  Manufacturer#1 d18GiDsL6Wm2IsGXM,RZf1jCsgZAOjNYVThTRP4 16-722-866-1658 quickly ironic sauternes use b 

9201.47 Supplier#000009690 UNITED KINGDOM 67183  Manufacturer#5 CB BnUTlmi5zdeEl7R7      33-121-267-9529 blithely unusual accounts integrate slyly. platelets 

9094.57 Supplier#000004582 RUSSIA   39575  Manufacturer#1 WB0XkCSG3r,mnQ n,h9VIxjjr9ARHFvKgMDf 32-587-577-1351 asymptotes above the slyly even requests haggle furiously about the regular accounts 

最後沒有(ps_supplycost = ...)的查詢返回:

s_acctbal s_name    n_name   p_partkey p_mfgr   s_address        s_phone   s_comment                       
--------- ------------------ -------------- --------- -------------- -------------------------------------- --------------- -------------------------------------------------------------------------------------------------- 
9984.69 Supplier#000008875 ROMANIA  13872  Manufacturer#3 hRdOqKqyU,sHq       29-132-904-4395 ironic instructions among the ironic requests boost at the ironic, ironic deposits. quick 

9955.05 Supplier#000008810 UNITED KINGDOM 73795  Manufacturer#2 ,Ot93zDXOFSjWSKsKrT7XJ4YPCP,A   33-527-478-5988 furiously unusual pinto beans nag ironic platelets. regular, bold fox 

9955.05 Supplier#000008810 UNITED KINGDOM 81285  Manufacturer#2 ,Ot93zDXOFSjWSKsKrT7XJ4YPCP,A   33-527-478-5988 furiously unusual pinto beans nag ironic platelets. regular, bold fox 

9938.53 Supplier#000005359 UNITED KINGDOM 185358 Manufacturer#4 QKuHYh,vZGiwu2FWEJoLDx04    33-429-790-6131 blithely silent pinto beans are furiously. slyly final deposits acros 

9937.84 Supplier#000005969 ROMANIA  108438 Manufacturer#1 ANDENSOSmk,miq23Xfb5RWt6dvUcvt6Qa  29-520-692-3537 carefully slow deposits use furiously. slyly ironic platelets above the ironic 

9936.22 Supplier#000005250 UNITED KINGDOM 249  Manufacturer#4 B3rqp0xbSEim4Mpy2RH J     33-320-228-2957 blithely special packages are. stealthily express deposits across the closely final instructi 

9923.77 Supplier#000002324 GERMANY  29821  Manufacturer#4 y3OD9UywSTOk       17-779-299-1839 quickly express packages breach quiet pinto beans. requ 

9871.22 Supplier#000006373 GERMANY  43868  Manufacturer#5 J8fcXWsTqM        17-813-485-8637 never silent deposits integrate furiously blit 

9870.78 Supplier#000001286 GERMANY  81285  Manufacturer#2 YKA,E2fjiVd7eUrzp2Ef8j1QxGo2DFnosaTEH 17-516-924-4574 final theodolites cajole slyly special, 

9870.78 Supplier#000001286 GERMANY  181285 Manufacturer#4 YKA,E2fjiVd7eUrzp2Ef8j1QxGo2DFnosaTEH 17-516-924-4574 final theodolites cajole slyly special, 

9852.52 Supplier#000008973 RUSSIA   18972  Manufacturer#2 t5L67YdBYYH6o,Vz24jpDyQ9    32-188-594-7038 quickly regular instructions wake-- carefully unusual braids into the expres 

9847.83 Supplier#000008097 RUSSIA   130557 Manufacturer#2 xMe97bpE69NzdwLoX      32-375-640-3593 slyly regular dependencies sleep slyly furiously express dep 

9847.57 Supplier#000006345 FRANCE   66344  Manufacturer#4 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques 

9847.57 Supplier#000006345 FRANCE   86344  Manufacturer#1 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques 

9847.57 Supplier#000006345 FRANCE   173827 Manufacturer#2 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques 

9836.93 Supplier#000007342 RUSSIA   4841  Manufacturer#4 JOlK7C1,7xrEZSSOw      32-399-414-5385 final accounts haggle. bold accounts are furiously dugouts. furiously silent asymptotes are slyly 

感謝

+0

你可以嘗試單個輸出查詢'select min(ps_supplycost)...'?我懷疑它由於其中的where子句而返回多行而不是一行。 –

+0

嘗試使用別名,以便它確切知道要引用哪個表,以防萬一它仍然引用主查詢中的表 - 即''從partsupp p2,供應商s2,國家n2,區域r2中選擇min(p2.ps_supplycost) p2.p_partkey = s2.ps_partkey'等 –

+0

我同意Mr Mr Fuzzy ...爲了便於閱讀,對於其他可能幫助您而不瞭解您的表格結構的人員,最好通過編寫您的查詢和alias.field引用來明確所有字段列查詢和加入/在哪裏條件。如果您可以請更新您的查詢將爲我或他人提供更好的幫助。 – DRapp

回答

0

看起來你正在努力尋找爲P_SIZE = 15的任何項目最便宜的供應成本和P_TYPE LIKE「%BRASS」 ......你SUBQUERY沒有這個限制...等等您的主要查詢可能包含100個產品(通過類似的方式),但是您的SUBQUERY可能有50,000個條目,因爲您不會將其限制爲僅具有相同產品類型的p_size = 15的相同條件。

您可能需要讓您的內部查詢還包含要應用MIN()的p_type和GROUP BY,然後將其作爲基礎加入。我用你命名約定的最佳表格/字段關係來重寫查詢...

此外,請注意我的內部查詢明確使用了alias.field名稱引用。希望不要讓外部查詢的相同字段名稱/值混淆。我可能並不確切,但認爲我接近你所尋找的原則。

select 
     s_acctbal, 
     s_name, 
     n_name, 
     p_partkey, 
     p_mfgr, 
     s_address, 
     s_phone, 
     s_comment, 
     p_type, 
     ps_supplycost 
    from 
     part 
     JOIN partsupp 
      ON p_partkey = ps_partkey 
      JOIN supplier 
       ON ps_suppkey = s_suppkey 
       JOIN nation 
        ON s_nationkey = n_nationkey 
        JOIN region 
        ON n_regionkey = r_regionkey 
        AND r_name = 'EUROPE' 

     JOIN (SELECT 
         P.p_type, 
         MIN(PS.ps_supplycost) MinCost 
        from 
         part P 
         JOIN partsupp PS 
          ON P.p_partkey = PS.ps_partkey 
          JOIN supplier S 
           ON PS.ps_suppkey = S.s_suppkey 
           JOIN nation N 
            ON S.s_nationkey = N.n_nationkey 
            JOIN region R 
            ON N.n_regionkey = R.r_regionkey 
            AND R.r_name = 'EUROPE' 
        where 
          P.p_size = 15 
         AND P.p_type like '%BRASS' 
        ) MinPerType 
      ON p_type = MinPerType.p_type 
      AND ps_supplycost = MinPerType.MinCost   
    where 
      p_size = 15 
     AND p_type like '%BRASS' 
    order by 
     s_acctbal desc, 
     n_name, 
     s_name, 
     p_partkey; 

因此,如您所見,檢測最低價格的內部查詢基於每個「P_TYPE」值。如果處理的銅項目,從樂器,或製造,水暖等黃銅金屬,每個「類型」可以有一個激烈的價格差異,你正在尋找例如:

BRASS #1 (small thing) that supply cost range $1.50 - $2.15 vs 
BRASS #83 (huge item) that supply cost range $42.00 - $48.50 

你永遠不會有一個BRASS #83永遠被挑選,因爲它從來沒有任何東西接近1.50美元的價格標籤成本。但是,您可以在$ 1.50範圍內擁有10個BRASS#1供應商,在$ 42個範圍內擁有7個BRASS#83項目供應商。00,所以你的最終查詢將返回17條記錄(根據這個例子)