MySQL IF ELSEIF in select query

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

    For your question :

    SELECT id, 
       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_2 && qty_3<='23'), price_3,
       IF('23' > qty_3, price_4, 1))))) as total 
    FROM product;
    

    You can use the if - else control structure or the IF function in MySQL.

    Reference:
    http://easysolutionweb.com/sql-pl-sql/how-to-use-if-and-else-in-mysql/

提交回复
热议问题