MySQL IF ELSEIF in select query

后端 未结 5 1999
無奈伤痛
無奈伤痛 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-23 00:12

    IF() in MySQL is a ternary function, not a control structure -- if the condition in the first argument is true, it returns the second argument; otherwise, it returns the third argument. There is no corresponding ELSEIF() function or END IF keyword.

    The closest equivalent to what you've got would be something like:

    IF(qty_1<='23', price,
      IF('23'>qty_1 && qty_2<='23', price_2,
        IF('23'>qty_2 && qty_3<='23', price_3,
          IF('23'>qty_3, price_4, 1)
        )
      )
    )
    

    The conditions don't all make sense to me (it looks as though some of them may be inadvertently reversed?), but without knowing what exactly you're trying to accomplish, it's hard for me to fix that.

提交回复
热议问题