Roman
Roman

Reputation: 575

MySQL if then/ case statement

there! I'm writing mysql script in mySQl Front 4.1.
I have problem with if then, case statements.

I have next code:

set @prodID = -1;
select @prodID = productID
from partid_to_productid 
where PartID= 8;


case @prodID
 WHEN NULL then select 0;
 else select 3;
 end case

Front doesn't want to execute it. Why? Can someone explain me what is wrong here?

Upvotes: 1

Views: 1969

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270795

The SELECT goes outside the CASE:

SELECT 
  CASE @prodID 
    WHEN NULL THEN 0
    ELSE 3
  END;

Actually, that's not returning 0 for me as I expect when testing. Instead try:

SELECT CASE WHEN @prodID IS NULL THEN 0 ELSE 3 END;

Upvotes: 4

Related Questions