Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Control Flow Functions in MySQL

    • 0
    • 3
    • 3
    • 1
    • 0
    • 0
    • 0
    • 0
    • 295
    Comment on it

    Mysql have several contral flow functions. Such as

    1) IF
    2) CASE
    3) IFNULL
    4) NULLIF

    Control Flow functions returns value based on processed each row by query executed. Contral Flow function can be used on **SELECT, WHERE, GROUP BY** and **ORDER BY**.

    1) IF
    This functions takes the three parameters as expression, If expression one is true then it will return second parameter otherwise return third parameter.

    Sample Queries:
      SELECT IF(expr1,expr2,expr3);
    
      // return yes
      SELECT IF(1<5,'yes','no');
    

    2) CASE
    We can say this like a switch case .

    Sample Queries:
      // Below will return zero
      SELECT CASE 0 WHEN 0 THEN 'zero' WHEN 1 THEN 'one' ELSE 'no one' END;
      
      // Below will return true
      SELECT CASE WHEN 5>2 THEN 'true' ELSE 'false' END;
    

    3) IFNULL
    This function takes two parameters as a expression , If expression one is not nul then it will return exprission one otherwise return two.

    Sample Queries:
      SELECT IFNULL(expr1, expr2);
      
      // Return 5
      SELECT IFNULL(5,0);
      
      // Return 10
      SELECT IFNULL(NULL,10);
    

    4) NULLIF
    This function takes two parameter as expression, If exp1=exp2 ,it will return true otherwise return exp1;

    Sample Queries:
      SELECT NULLIF(expr1,expr2);
      
      // Return NULL
      SELECT NULLIF(5,5);
      
      // Return 10
      SELECT NULLIF(10,4);

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: