如果,请不要返回最低值

时间:2013-09-03 16:55:33

标签: mysql sql

目标

不要退还市场暂停的最低价格。

问题

我不知道语法。

场景

以下存储过程可获得特定产品的最低最大价格:

BEGIN
    Select Min(Case When product.PromotionalPrice = 0
            Then product.OriginalPrice Else
            Least(product.PromotionalPrice, product.OriginalPrice)
            End) As minProductPrice,
       Max(Case When product.PromotionalPrice = 0
            Then product.OriginalPrice Else
            Least(product.PromotionalPrice, product.OriginalPrice)
            End) As maxProductPrice
    From products As product
    Where product.Name = 'Playstation 3';
END

背景是:市场产品。产品属于市场。如果某个市场被暂停,那么它不会显示其产品,也不会将它们添加到最大/最小价格比较中。

你们都明白吗? 我想将其市场暂停的产品排除在上述查询的MinMax声明之外。

表格

这是markets表:

+----+------+-------------+
| Id | Name | SituationId |
+----+------+-------------+
| 1  | A    | 1           |
+----+------+-------------+
| 2  | B    | 2           |
+----+------+-------------+
| 3  | C    | 3           |
+----+------+-------------+

这是markets_situations表:

+----+-----------+
| Id | Name      |
+----+-----------+
| 1  | Neutral   |
+----+-----------+
| 2  | Premium   |
+----+-----------+
| 3  | Suspended |
+----+-----------+

最后,这是products表:

+----+---------------+--------+------------------+---------------+
| Id | Name          | Market | PromotionalPrice | OriginalPrice |
+----+---------------+--------+------------------+---------------+
| 1  | Xbox 360      | 1      | 0                | 225,00        |
+----+---------------+--------+------------------+---------------+
| 2  | Xbox 360      | 2      | 99,00            | 175,00        |
+----+---------------+--------+------------------+---------------+
| 3  | Xbox 360      | 3      | 0                | 135,00        |
+----+---------------+--------+------------------+---------------+
| 4  | Playstation 3 | 1      | 0                | 189,00        |
+----+---------------+--------+------------------+---------------+
| 5  | Playstation 3 | 2      | 125,00           | 165,00        |
+----+---------------+--------+------------------+---------------+
| 6  | Playstation 3 | 3      | 110,00           | 185,00        |
+----+---------------+--------+------------------+---------------+

增强理解力

我不希望将110,00显示为存储过程结果的Min价格,因为其市场(C)为Suspended

我已经做了什么

我已经尝试了以下内容,但没有成功:

BEGIN
    [...]

    Where product.Name = 'Playstation 3'
    And marketSituation.Id <> 3;
END

会发生什么? And条件无效。查询一直让我回到暂停市场的价格。

2 个答案:

答案 0 :(得分:1)

Select Min(Case When product.PromotionalPrice = 0
        Then product.OriginalPrice Else
        Least(product.PromotionalPrice, product.OriginalPrice)
        End) As minProductPrice,
   Max(Case When product.PromotionalPrice = 0
        Then product.OriginalPrice Else
        Least(product.PromotionalPrice, product.OriginalPrice)
        End) As maxProductPrice
From products As product
Inner join markets on product.market = markets.id AND markets.SituationId <> 3
Where product.Name = 'Playstation 3';

答案 1 :(得分:0)

这样的东西
Select Min(Case When product.PromotionalPrice = 0
        Then product.OriginalPrice Else
        Least(product.PromotionalPrice, product.OriginalPrice)
        End) As minProductPrice,
   Max(Case When product.PromotionalPrice = 0
        Then product.OriginalPrice Else
        Least(product.PromotionalPrice, product.OriginalPrice)
        End) As maxProductPrice
From products As product INNER JOIN
Markets ON Product.Market = Markets.Id
Where product.Name = 'Playstation 3'
AND Markets.SituationID <> 3