MySQL:我的MIN()和MAX()查询可以/应该组合吗?

时间:2012-09-26 18:30:41

标签: mysql sql

这是我正在做的简化版本。

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

有没有一种方法可以节省一些资源,也可以将这些资源合并到更少的查询中? 或者这几乎是这样做的方式吗?

[更新]
以下是一些可以使用的示例数据:

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);

最终,我们希望将所有数据打包成一个数组 目前,我们从每个查询(没有GROUPing)获取一行,其中我们有实际的 ID Qty ...具有最小值的单行,以及具有最大价格的单行的相同信息,每个“类型”类别,然后构建数组。
例如:

$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

现在,我从各个查询中获取所有数据,然后将它们全部组合成一个大型数组。 我认为 得到 是更好的方法:)

4 个答案:

答案 0 :(得分: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字段,因为我认为你不打算将聚合分组在键上,并且想知道你为什么要将它们分组在数量上。

答案 1 :(得分: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 

答案 2 :(得分: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

答案 3 :(得分: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