IF ELSE statement allows performing different actions based on conditions in a SQL query. It is used for conditional checking and executes the statement specified in THEN or ELSE based on the result. The syntax is: IF condition THEN operation 1 ELSE operation 2 END IF. Example: SELECT Product, IF(Product = 'Book', 'Book', 'Magazine') AS ProductType FROM Sales; Displays a "Book" or "Magazine" message based on the value of the Product column.
Usage of IF ELSE statement in SQL
The IF ELSE statement allows you to perform conditional checks in SQL queries. and perform different actions based on the results.
Syntax:
<code>IF 条件 THEN 操作1 ELSE 操作2 END IF;</code>
Example:
Suppose you have a table named "Sales" that contains each "Product" and "Amount" columns for each order. To check whether the product type in each order is "Book" or "Magazine" and display a different message accordingly, you can use the following IF ELSE statement:
<code>SELECT Product, IF(Product = 'Book', '这是订购的书籍', '这是订购的杂志') AS ProductType FROM Sales;</code>
Explanation:
Product = 'Book'
. THEN
operation: display "This is the ordered book". ELSE
action: display "This is the magazine ordered". Note:
The above is the detailed content of Usage of if else in sql. For more information, please follow other related articles on the PHP Chinese website!