Alvins
Alvins

Reputation: 887

mySQL select which query evaluate by parameter

I've a query like this one:

SELECT IF(@param = 42, 'static', SELECT ... );

But it doesn't work because I can't insert a SELECT statement inside a IF(). My problem is that I can't do otherwise (use an if-then statement outside sql) because to "architecture restrictions".

Any solution to select if evaluate or not a query based to parameter value?

Upvotes: 0

Views: 59

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269643

You don't need a select:

select (case when @param = 42 then 'static' else date_format(now(), '%Y-%m-%d') end)

Note that you are trying to mix two different types -- a datetime and string. You should explicitly convert the datetime to a string, using your preferred format.

You can write this with if(). case is slightly more general and the ANSI standard.

Upvotes: 2

Related Questions