vendredi 26 février 2016

MySQL IF return multiple values?

How can I return multiple values from an IF statement in MySQL? Embedding the IF into the query is not an option.

SET @values = IF(@year = 2014, (2, 3, 4), 1); # either (2, 3, 4) OR 1
SELECT * FROM whatever WHERE `column` IN (@values);

Error Code: 1241. Operand should contain 1 column(s)

One solution would be to use quotation marks and store that value to a variable, but it does not work with the IN command, it needs raw numbers:

SELECT IF (24 IN ("23,24,25"), TRUE, FALSE) AS `does-it-contain`;

FALSE

Aucun commentaire:

Enregistrer un commentaire