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

MySQL:我的MIN()和MAX()查询可以/应该合并吗?,mysql,sql,Mysql,Sql,这是我目前正在做的一个简化版本 (A) 最终,我们希望将所有数据很好地打包到一个数组中。 目前,我们从每个查询(无分组)中获得一行,其中包含实际ID、数量等。。。对于具有最小值的单行,以及对于具有最大价格的单行,每个“类型”类别的相同信息,然后构建数组。 例如: $flash_report = array( 'tier1' => array( 'qty' => array( 'min' => array(

这是我目前正在做的一个简化版本

(A)

最终,我们希望将所有数据很好地打包到一个数组中。
目前,我们从每个查询(无分组)中获得一行,其中包含实际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]

现在,我正在从我们的单个查询中获取所有数据,然后将其组装成一个大的ol'数组。
我认为有一种更好的方法可以做到这一点:)

选择id,
外键id,
类型,
量
最大值(当类型为'tier 1'时,则为price else NULL end)作为price_MAX_eq,
最小值(当类型为'tier 1'时,则为price else NULL end)作为price_MIN_eq,
最大值(当类型为“tier 1”时,则为price else NULL end)作为price_MAX_neq,
最小值(当类型为“tier 1”时,则为price else NULL end)作为price_MIN_neq,
引述
选择id,
外键id,
类型,
量
最大值(当类型为'tier 1'时,则为price else NULL end)作为price_MAX_eq,
最小值(当类型为'tier 1'时,则为price else NULL end)作为price_MIN_eq,
最大值(当类型为“tier 1”时,则为price else NULL end)作为price_MAX_neq,
最小值(当类型为“tier 1”时,则为price else NULL end)作为price_MIN_neq,
引述

您至少可以将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

你至少可以把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

您可以将这一切结合起来:

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
与案例解决方案相比,我更喜欢它(尽管它们更简洁),因为它完全符合SQL92


我删除了id、foreign_key和qty字段,因为我认为您不打算将聚合分组到键上,并且想知道为什么您也希望将它们分组到qty上

您可以将这一切结合起来:

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
与案例解决方案相比,我更喜欢它(尽管它们更简洁),因为它完全符合SQL92


我删除了id、foreign_key和qty字段,因为我认为您不打算将聚合分组到键上,并且想知道为什么您也希望将它们分组到qty上

假设查询是简化的/对象化的,但像我这样的人会把注意力集中在缺少
groupby
,这意味着由于@omg ponies,结果可能是任意的。@omg ponies,谢谢。这个问题现在已经更新了更多信息,以更好地阐明我试图实现的目标。顺便说一句,我把期望的结果作为一个数组。这不仅是因为我计划如何打包数据,而且(主要是)因为我认为这是最容易理解的。。。我希望其他人也是这样:)假设查询是简化的/对象化的,但像我这样的人会关注缺少
groupby
,这意味着结果可能是任意的。@omg小马谢谢。这个问题现在已经更新了更多信息,以更好地阐明我试图实现的目标。顺便说一句,我把期望的结果作为一个数组。这不仅是因为我计划如何打包数据,而且(主要是)因为我认为这是最容易理解的。。。我希望其他人也是这样:)在这一点上,我会在原始问题中的4个单独语句之间加上一个并集,并添加一个名为“条件”的新字段(如我的回答中所示),其中包含4个不同的值“min_notequal”、“min_equal”、max_notequal、“max_equal”。此时,我将在原始问题中的4个单独语句之间插入一个并集,并添加一个名为“条件”的新字段(如我的回答中所示),其中包含4个不同的值“min_notequal”、“min_equal”、max_notequal、“max_equal”。请提供示例数据和所需输出。请提供示例数据和所需输出。
$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
                )
        )
    )
)
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 
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
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
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