sql >> Database >  >> RDS >> Mysql

Geneste CASE-instructies in MySQL

Je mist een THEN in je eerste CASE Uitspraak. (sorry dat ik tabelaliassen moest toevoegen )

SELECT sc.session_id
    , sc.product_id
    , sc.product_qty
    , sc.product_option
    , p.product_title
    , p.product_price
    , p.product_sale_price_status
    , p.product_sale_price
    , po.option_text
    , po.option_upcharge
    , CASE
        WHEN (p.product_sale_price_status = 'Y')
        THEN <-- add this
            CASE
            WHEN (po.option_upcharge IS NOT NULL)
                THEN (sc.product_qty * (p.product_sale_price + po.option_upcharge)) 
                ELSE (sc.product_qty * p.product_sale_price)    
            END
        ELSE
            CASE
            WHEN (po.option_upchage IS NOT NULL)
                THEN (sc.product_qty * (p.product_price + po.option_upcharge))
                ELSE (sc.product_qty * p.product_price)
            END
        END AS product_total
FROM tblshopping_cart sc
INNER JOIN tblproducts p
    ON sc.product_id = p.product_id
LEFT JOIN tblproduct_options po
    ON sc.product_option = po.option_product_id
ORDER BY sc.product_qty ASC


  1. Selecteer eerste record als er geen overeenkomen

  2. Een postgres-back-upbestand herstellen via de opdrachtregel?

  3. Installeer PostgreSQL op Ubuntu 18.04

  4. NLS_CHARSET_NAME() Functie in Oracle