2012-09-26 92 views
0

這是簡化版我正在做的事情。MySQL:可以/我的MIN()和MAX()查詢結合起來嗎?

A)

SELECT id, foreign_key_id, type, quantity, MAX(price) AS price // <--- Max 
FROM quotes 
WHERE type = 'tier 1'           // <--- Equal 
AND prod_id_requested = 12345 

B)

SELECT id, foreign_key_id, type, quantity, MAX(price) AS price // <--- Max 
FROM quotes 
WHERE type != 'tier 1'           // <--- NOT equal 
AND prod_id_requested = 12345 

C)

SELECT id, foreign_key_id, type, quantity, MIN(price) AS price // <--- Min 
FROM quotes 
WHERE type = 'tier 1'           // <--- Equal 
AND prod_id_requested = 12345 

d)

SELECT id, foreign_key_id, type, quantity, MIN(price) AS price // <--- Min 
FROM quotes 
WHERE type != 'tier 1'           // <--- NOT equal 
AND prod_id_requested = 12345 

有沒有一種好方法可以保存一些資源,並且可以將這些資源合併爲更少的查詢?
或者這是相當多的方式來做到這一點?

[UPDATE]
下面是一些樣本數據一起玩:

CREATE TABLE IF NOT EXISTS `quote_item` (
    `id` int(2) unsigned NOT NULL AUTO_INCREMENT, 
    `quote_id` int(2) unsigned NOT NULL, 
    `product_id_quoted` int(3) unsigned NOT NULL, 
    `product_id_requested` int(3) unsigned NOT NULL, 
    `type` varchar(52) DEFAULT NULL, 
    `price` float(10,4) NOT NULL, 
    `quantity` int(9) unsigned NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; 

INSERT INTO `quote_item` (`id`, `quote_id`, `product_id_quoted`, `product_id_requested`, `type`, `price`, `quantity`) VALUES 
(1, 1, 12, 12, 'tier 1', 0.0100, 100), 
(2, 2, 1, 12, 'tier 3', 0.0038, 8200), 
(3, 2, 13, 12, 'tier 2', 0.0041, 10000), 
(4, 3, 7, 14, 'tier 1', 0.0060, 25000), 
(5, 3, 8, 12, NULL, 0.0180, 250), 
(6, 3, 9, 15, NULL, 0.0019, 5000), 
(7, 4, 12, 12, NULL, 0.0088, 7500), 
(8, 4, 16, 12, 'tier 1', 0.0040, 10000), 
(9, 5, 12, 9, 'tier 2', 0.0089, 1200), 
(10, 5, 12, 12, 'tier 1', 0.0072, 6400); 

最終,我們希望所有的數據很好地收拾到一個數組。
目前,我們正在從每個查詢得到一個行(不分組),我們有實際ID數量 ...爲單行的最小值,以及相同的信息對於具有最大價格的單行,按「類型」類別,然後構建數組。
例如:

$flash_report = array(
    'tier1' => array(
     'qty' => array(
      'min' => array(
       'id' => 1 
       ,'quote_id' => 1 
       ,'price' => 0.01 
       ,'quantity' => 100 
       ) 
      ,'max' => array(
       'id' => 8 
       ,'quote_id' => 4 
       ,'price' => 0.004 
       ,'quantity' => 10000 
       ) 
      ,'sum' => array(
       'value' => 16500 
       ,'count' => 3 
       ) 
     ) 
     ,'price' => array(
      'min' => array(
       'id' => 8 
       ,'quote_id' => 4 
       ,'price' => 0.004 
       ,'quantity' => 10000 
       ) 
      ,'max' => array(
       'id' => 1 
       ,'quote_id' => 1 
       ,'price' => 0.01 
       ,'quantity' => 100 
       ) 
     ) 
    ) 
    ,'other' => array(
     'qty' => array(
      'min' => array(
       'id' => 5 
       ,'quote_id' => 3 
       ,'price' => 0.018 
       ,'quantity' => 250 
       ) 
      ,'max' => array(
       'id' => 3 
       ,'quote_id' => 2 
       ,'price' => 0.0041 
       ,'quantity' => 10000 
       ) 
      ,'sum' => array(
       'value' => 25950 
       ,'count' => 4 
       ) 
     ) 
     ,'price' => array(
      'min' => array(
       'id' => 2 
       ,'quote_id' => 2 
       ,'price' => 0.0038 
       ,'quantity' => 8200 
       ) 
      ,'max' => array(
       'id' => 5 
       ,'quote_id' => 3 
       ,'price' => 0.018 
       ,'quantity' => 250 
       ) 
     ) 
    ) 
) 

[/UPDATE]

現在,我是從我們個人的查詢得到的所有數據,然後將其全部組裝成一個大醇」陣列。
我想有是一個更好的方式來做到這一點:)

+1

授予查詢簡化/ obfusicated,但像我這樣的人將專注於缺乏'GROUP BY'這意味着結果可能是任意的,因爲[MySQL的GROUP BY支持](http://dev.mysql.com/ DOC/refman/5.0/EN /組逐隱藏columns.html)。 –

+0

@ omg-ponies謝謝。這個問題現在已經更新了更多的信息,以更好地闡明我想要達到的目標。 – mOrloff

+0

順便說一句,我把所需的結果作爲一個數組。不僅因爲這是我計劃編制數據的方式,還因爲我認爲這是最容易理解的。 ...我希望對其他人也是如此:) – mOrloff

回答

2
SELECT id, foreign_key_id, type, quantity, 
    MIN(case when type != 'tier 1' then price end) as MinNotTier1Price, 
    MIN(case when type == 'tier 1' then price end) as MinTier1Price, 
    MAX(case when type != 'tier 1' then price end) as MaxNotTier1Price, 
    MAX(case when type == 'tier 1' then price end) as MaxTier1Price 
FROM quotes 
+0

請提供樣本數據和期望的輸出。 – RedFilter

+0

謝謝。這個問題已經更新了這個信息。 – mOrloff

1
SELECT id, 
     foreign_key_id, 
     type, 
     quantity, 
     MAX(case when type = 'tier 1' then price else NULL end) AS price_max_eq, 
     MIN(case when type = 'tier 1' then price else NULL end) AS price_min_eq, 
     MAX(case when type <> 'tier 1' then price else NULL end) AS price_max_neq, 
     MIN(case when type <> 'tier 1' then price else NULL end) AS price_min_neq, 
FROM quotes 
0

您至少可以結合A和C以及B和d像這樣

SELECT id, 
     foreign_key_id, 
     type, 
     quantity, 
     MAX(price) AS max_price, 
     MIN(price) AS min_price 
FROM quotes 
WHERE type = 'tier 1' 
GROUP BY id, foreign_key_id, type, quantity 


SELECT id, 
     foreign_key_id, 
     type, 
     quantity, 
     MAX(price) AS max_price, 
     MIN(price) AS min_price 
FROM quotes 
WHERE type != 'tier 1' 
GROUP BY id, foreign_key_id, type, quantity 
3

您可以結合這一切:

SELECT "equal" as condition, 
     MAX(price) AS max_price, 
     MIN(price) AS min_price 
    FROM quotes 
WHERE type = 'tier 1' 
GROUP BY 1 
UNION 
SELECT "not_equal" as condition, 
     MAX(price) as max_price, 
     MIN(price) as min_price 
    FROM quotes 
WHERE type != "tier 1" 
GROUP BY 1 

我比CASE解決方案更好(雖然那些更簡潔),因爲它完全符合SQL92。

我刪除了ID,foreign_key和qty字段,因爲我沒有想到你打算將聚合按鍵分組,並且想知道爲什麼你想讓它們也按照數量分組。

+0

在這一點上,我會在你的原始問題中的4個單獨的陳述之間插入一個UNION,並在4個不同的值「min_notequal」,「min_equal」中添加一個名爲「condition」的新字段(如我的回答) max_notequal,「max_equal」。 –

相關問題