返回特定列的情况

时间:2013-06-20 18:39:53

标签: c# mysql sql asp.net-mvc-4

目标

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

问题

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

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

详细

我正在使用C#.NET + MVC 4 + MySQL +实体框架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

此程序返回以下内容:

Image

有什么想法吗?

1 个答案:

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