MySQL IF ELSEIF in select query

后端 未结 5 1981
無奈伤痛
無奈伤痛 2020-12-22 23:33

I\'m trying to select different prices of a product based on the quantity that user chooses. This is the query I\'m working on (it has a syntax error):

 sele         


        
5条回答
  •  攒了一身酷
    2020-12-22 23:59

    I found a bug in MySQL 5.1.72 when using the nested if() functions .... the value of column variables (e.g. qty_1) is blank inside the second if(), rendering it useless. Use the following construct instead:

    case 
      when qty_1<='23' then price
      when '23'>qty_1 && qty_2<='23' then price_2
      when '23'>qty_2 && qty_3<='23' then price_3
      when '23'>qty_3 then price_4
      else 1
    end
    

提交回复
热议问题