Josef
Josef

Reputation: 2726

If..then statement in mysql

I have stored procedure with one parameter called paramType (INT)

I'm wondering is there any way how to implement next situation in mysql:

BEGIN
SELECT Code, Price, Type  FROM table
IF paramType IS NOT NULL THEN
WHERE Type = paramType
ELSE
SELECT Code, Price Type  FROM table
END;

So, if paramType is null (empty) then just execute SELECT Code, Price, Type FROM table but if paramType contains value than execute query with condition like:

SELECT Code, Price Type  FROM table WHERE Type = paramType

Or is there other solution without if/then/else?

Upvotes: 1

Views: 82

Answers (1)

sagi
sagi

Reputation: 40481

Here is the more elegant way of writing this :

SELECT Code, Price, Type 
FROM table
WHERE paramType IS NULL OR Type = paramType 

Upvotes: 4

Related Questions