jeudi 8 octobre 2015

If statement is 1 then run specific SQL statement

I trying to learn MySQL stored procedures, and I'm stuck in a If..Then matter. I'm trying to figure out how to show columns do to the matter of a result from the IF.

Here is my code, hope you understand what i'm trying to do, my issue is between "..." and "..." :

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_helloworld`()
BEGIN

    SELECT
        ...
        IF pt.is_part_payment = 1 THEN
            pp.due_date AS due_date,
            pp.amount AS amount
        ELSE
            o.due_date AS due_date,
            o.amount AS amount
        END IF;
        ...
    FROM
        orders o
        INNER JOIN payment_types pt ON pt.id = o.payment_type_id 
        INNER JOIN part_payments pp ON pp.order_id = o.order_id
    WHERE
        o.active = 1
END

Aucun commentaire:

Enregistrer un commentaire