代码之家  ›  专栏  ›  技术社区  ›  Derek Adair

如何处理此特定查询

  •  0
  • Derek Adair  · 技术社区  · 15 年前

    我有一个相当复杂的逻辑查询要执行。基本上有一系列相关的表。我很难找到一个接近它的好方法。

    桌子:

    交易-
    TyId

    折扣-
    DYID
    项目编号
    类型(百分比或基本值)
    价值

    交易折扣-
    TyId
    DYID

    项目-
    项目编号
    价格

    编辑:附加表
    购买
    t_id号
    项目编号

    我正试图在应用折扣后调整价格。我想避免在数据库中添加另一列调整后的价格…因为可以根据已经输入的数据计算每个交易的调整价格(因此存储这些数据是多余的)。

    这是基本逻辑:

    if(this transaction had a discount applied)  
      //apply the discount and return the adjusted price  
    else(no discount applied)  
      //pull price
    

    可以使用PHP逻辑在几个单独的查询中执行此操作。

    //1st step - create an array of bool's: true = discount_used/false = no_discount
    //2nd step - if(discount_used) return price adjusted to discount
    //3rd step - if(no_discount) return price
    //4th step - combine the two different arrays
    

    这显然很笨重。似乎必须有一种更好的方法来做到这一点,这可能会更有效率。

    据我所知,您可以在MySQL中执行包含逻辑的查询,这有帮助吗?

    7 回复  |  直到 15 年前
        1
  •  2
  •   Adriaan Stander    15 年前

    假设您在Transactions表中有一个item_id,并且该百分比由“p”表示,您可以尝试类似的方法

    SELECT  *,
            CASE 
                WHEN td.T_id IS NULL
                    THEN it.price
                WHEN td.T_id IS NOT NULL AND d.Type = 'P'
                    THEN id.price - (id.price * d.value)
                ELSE id.price - d.value
            END PriceDiscounted
    
    FROM    Transaction t LEFT JOIN
            Purchase p ON t.T_id = p.T_id LEFT JOIN
            Item it ON p.item_id = it.item_id LEFT JOIN
            Transaction_Discount td ON t.T_id = td.T_id LEFT JOIN
            Discount d ON td.D_id = d.D_id LEFT JOIN
            Item id ON d.item_id = id.item_id
    
        2
  •  1
  •   Chris Kannon    15 年前

    德里克,我建议使用一个存储函数。创建函数后,您可以简单地执行以下操作:

    select item_id, price_after_discount(item_id) from item where ....
    
        3
  •  1
  •   Marius    15 年前

    有一个 if statement in mysql 你可以用的。例如:

    Select item_id, price*if(discount.type, discount.value, 1) 
    from item join discount on item.item_id = discount.item_id
    

    您可能需要在查询在所有情况下工作之前处理它,但它应该工作。

        4
  •  1
  •   D'Arcy Rittich    15 年前

    尝试以下方法:

    select i1.item_id, 
        i1.Price, 
        case when dt.item_id is null then i1.Price else i1.Price - dt.TotalDiscount end as DiscountedPrice
    from Item i1
    left outer join (
        select i.item_id, sum(
            case when d.type = 'percentage' then i.price * d.value / 100 else d.value end
        ) as TotalDiscount
        from Item i
        inner join Discount d on i.item_id = d.item_id
        group by i.item_id
    ) dt on i1.item_id = dt.item_id
    
        5
  •  0
  •   SorcyCat    15 年前

    只要您可以创建一个具有原始价格的查询(可能是左联接),并且如果存在折扣价格,则可以使用if语句来选择折扣价格(如果存在)。

    http://dev.mysql.com/doc/refman/5.1/en/if-statement.html

        6
  •  0
  •   David Oneill    15 年前

    我觉得你做不到。对于一个特定的t_id,如果没有折扣,您就无法加入到item_id,因为只有item_id的表是 Item 表与 Discount 表。

    如果要进行不带折扣的交易,则需要更改表的设置方式。

    您可以尝试如下操作:

    Item table:
      item_id
      price
    
    Transaction table:
      T_id
      item_id
      D_id
    
    Discount table:
      D_id
      type
      value
    

    除非你想对一个特定项目的每一个实例应用一个特定的折扣。然后你会想做如下的事情:

    Item table:
      item_id
      price
      D_id (can be null)
    
    Transaction table:
      T_id
      item_id
    
    Discount table:
      D_id
      type
      value
    
        7
  •  0
  •   Nathan    15 年前

    另一种方法-将这两个条件作为单独的查询执行,并将结果联合起来,例如沿着的行(当然要针对您的表进行调整)

    SELECT unadjustedPrice as Price FROM Transaction WHERE noDiscount = true
    UNION ALL
    SELECT unadjustedPrice - discount as Price From Transaction WHERE noDiscount = false
    

    当然,真正的查询将更加复杂,因为您需要加入适当的表,但这将为您提供一般的方法。