No cache version.

Caching disabled. Default setting for this page:enabled (code DEF204)
If the display is too slow, you can disable the user mode to view the cached version.

Rechercher dans le manuel MySQL

13.6.5.2 IF Syntax

  1. IF search_condition THEN statement_list
  2.     [ELSEIF search_condition THEN statement_list] ...
  3.     [ELSE statement_list]

The IF statement for stored programs implements a basic conditional construct.

Note

There is also an IF() function, which differs from the IF statement described here. See Section 12.4, “Control Flow Functions”. The IF statement can have THEN, ELSE, and ELSEIF clauses, and it is terminated with END IF.

If a given search_condition evaluates to true, the corresponding THEN or ELSEIF clause statement_list executes. If no search_condition matches, the ELSE clause statement_list executes.

Each statement_list consists of one or more SQL statements; an empty statement_list is not permitted.

An IF ... END IF block, like all other flow-control blocks used within stored programs, must be terminated with a semicolon, as shown in this example:

  1. DELIMITER //
  2.  
  3. CREATE FUNCTION SimpleCompare(n INT, m INT)
  4.  
  5.     DECLARE s VARCHAR(20);
  6.  
  7.     IF n > m THEN SET s = '>';
  8.     ELSEIF n = m THEN SET s = '=';
  9.     ELSE SET s = '<';
  10.     END IF;
  11.  
  12.     SET s = CONCAT(n, ' ', s, ' ', m);
  13.  
  14.     RETURN s;
  15.   END //
  16.  
  17. DELIMITER ;

As with other flow-control constructs, IF ... END IF blocks may be nested within other flow-control constructs, including other IF statements. Each IF must be terminated by its own END IF followed by a semicolon. You can use indentation to make nested flow-control blocks more easily readable by humans (although this is not required by MySQL), as shown here:

  1. DELIMITER //
  2.  
  3. CREATE FUNCTION VerboseCompare (n INT, m INT)
  4.  
  5.     DECLARE s VARCHAR(50);
  6.  
  7.     IF n = m THEN SET s = 'equals';
  8.     ELSE
  9.       IF n > m THEN SET s = 'greater';
  10.       ELSE SET s = 'less';
  11.       END IF;
  12.  
  13.       SET s = CONCAT('is ', s, ' than');
  14.     END IF;
  15.  
  16.     SET s = CONCAT(n, ' ', s, ' ', m, '.');
  17.  
  18.     RETURN s;
  19.   END //
  20.  
  21. DELIMITER ;

In this example, the inner IF is evaluated only if n is not equal to m.


Find a PHP function

Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-if.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:en Manuel MySQL : https://dev.mysql.com/

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut