jeudi 3 mai 2018

MYSQL stored procedure, if statement, ifnull

I'm trying to create a stored procedure with mysql. The idea is not check if the first request is empty or not, and do it if it's not, or do the second one if the first is empty.

My code, symplified version.

DELIMITER //

CREATE PROCEDURE HFTester(idTest INT)
BEGIN 
    IF (SELECT * FROM `foo` WHERE id = idTest) != NULL
    THEN (SELECT * FROM `foo` WHERE id = idTest;)
    ELSE (SELECT * FROM `bar` WHERE id = idTest;)
    END IF;
END //

DELIMITER;  

The procedure is saved, but when I execute it, i get an empty result.

So I searched the Internet, and I found ifnull .

I then tried to apply it to my case:

DELIMITER //

CREATE PROCEDURE HFTester(idFiche INT)
BEGIN 
    SELECT IFNULL(
            (SELECT * FROM `foo` WHERE id = idTest;),
            (SELECT * FROM `bar` WHERE id = idTest;)
        );
END //
DELIMITER;

I'm pretty sure the IFNULL can't work, but is there a way to do this if statement in MySQL, or am I force to do it in php?

Victor.

Aucun commentaire:

Enregistrer un commentaire