3

目标

如果产品是报价并且价格最低,则返回特定列。

问题

我正在创建一个产品价格比较的应用程序。我已经获得了产品列表和特定产品的最低价格。但是,在一天之内,该产品可以设置为报价,并且具有所有其他产品的最低价格。发生这种情况时,我必须向用户识别此类信息——使用标志或类似的东西。

我正在考虑使用查询本身来执行此行为 - 但我不知道,有时这种做法似乎不一致,因为如果产品不销售,不知道我将如何在 C# 中处理这个问题等待productState专栏。我需要建议(当然还有语法,因为我 [再次] 不知道)。

细节

我正在使用 C#.NET + MVC 4 + MySQL + Entity Framework 5 + 存储过程 + Razor 引擎。

以下片段返回我要比较的产品列表的最低价格:

Min(Case When marketProducts.ProductPromotionalPrice = 0
    Then 
       marketProducts.ProductOriginalPrice 
    Else
       Least(
        marketProducts.ProductPromotionalPrice,
        marketProducts.ProductOriginalPrice
       )End) As minProductPrice,

但是,我想做这样的事情(当然——语法是错误的,但是“有效”来说明):

Min(Case When marketProducts.ProductPromotionalPrice = 0
    Then 
       marketProducts.ProductOriginalPrice 
    Else
       marketProducts.ProductState = 1 As productState, /* 
                                                           This is a normal
                                                           column.
                                                           1 means that 
                                                           the product is on 
                                                           offer.
                                                        */
       Least(
        marketProducts.ProductPromotionalPrice,
        marketProducts.ProductOriginalPrice
       )End) As minProductPrice,

完整的查询:

CREATE DEFINER=`root`@`localhost` PROCEDURE `getProductsList`(IN `categoryId` INT, IN `productState` INT)
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
    Select product.Id As productId,
        product.Name As productName,
        Min(Case When marketProducts.ProductPromotionalPrice = 0
            Then marketProducts.ProductOriginalPrice Else
            Least(marketProducts.ProductPromotionalPrice, marketProducts.ProductOriginalPrice)
            End) As minProductPrice,
            measure.Name As measureName,
            measure.Abbreviation As measureAbbreviation,
            productsImages.ProductImageThumbnail As thumbnailUrl,
            product.InMarketsQuantity as numberOfMarketsThatHaveThisProduct
    From bm_market_products as marketProducts
    Join bm_products As product On marketProducts.ProductId = product.Id
    Join bm_categories As category On product.CategoryId = category.Id
    Join bm_measures As measure On product.MeasureId = measure.Id
    Join bm_products_images As productsImages On product.Id = productsImages.ProductId
    WHERE (productState Is Null Or marketProducts.ProductState = productState)
    And (categoryId Is Null Or category.Id = categoryId)
    Group By marketProducts.ProductId;
END

此过程返回以下内容:

图片

有任何想法吗?

4

1 回答 1

1

您的语法错误的原因是您有时将两列填充在一列的空间中。解决方案是始终拥有两列,如下所示:

Select product.Id As productId,
    product.Name As productName,
    Min(Case When marketProducts.ProductPromotionalPrice = 0
        Then marketProducts.ProductOriginalPrice Else
        Least(marketProducts.ProductPromotionalPrice, marketProducts.ProductOriginalPrice)
        End) As minProductPrice,
    Min(Case When marketProducts.ProductPromotionalPrice = 0
            and marketProducts.ProductPromotionalPrice < marketProducts.ProductOriginalPrice
        Then 0 Else 1
        End) As productState,
        measure.Name As measureName,
        measure.Abbreviation As measureAbbreviation,
        productsImages.ProductImageThumbnail As thumbnailUrl,
        product.InMarketsQuantity as numberOfMarketsThatHaveThisProduct
From bm_market_products as marketProducts
Join bm_products As product On marketProducts.ProductId = product.Id
Join bm_categories As category On product.CategoryId = category.Id
Join bm_measures As measure On product.MeasureId = measure.Id
Join bm_products_images As productsImages On product.Id = productsImages.ProductId
WHERE (productState Is Null Or marketProducts.ProductState = productState)
And (categoryId Is Null Or category.Id = categoryId)
Group By marketProducts.ProductId;
于 2013-06-21T23:45:36.060 回答