Home > Net >  MySQL Where condition with CASE
MySQL Where condition with CASE

Time:08-05

WHERE CASE WHEN cksp.status IS NOT NULL THEN ckspc.is_include_in_pipeline = 1 
      ELSE 1=1 
      END

Here i want like if cksp.status is not null then condition should be cksp.is_include_in_pipeline = 1 ELSE ignore the where condition

CodePudding user response:

Skip the case expression, use regular AND/OR instead,

WHERE cksp.status IS NULL
   OR ckspc.is_include_in_pipeline = 1 

CodePudding user response:

That logic should be similar to:

(cksp.status IS NOT NULL and ckspc.is_include_in_pipeline = 1) or cksp.status IS NULL

CodePudding user response:

WHERE ckspc.is_include_in_pipeline = CASE WHEN cksp.status IS NOT NULL THEN  1 
      ELSE 9999999
      END

or

WHERE ckspc.is_include_in_pipeline =  1 
or ckspc.status IS NOT NULL

DEMO WITH ONLY ONE TABLE

CodePudding user response:

Try pasting your query onto EverSQL.com It can rewrite your query and optimize it for performance.

  • Related