Home > Article > Daily Programming > How to use oracle decode function in mysql
In MySQL, you can use CASE expressions to implement functions similar to Oracle's DECODE function: determine conditions and return values; use CASE expressions to list conditions and return values; add ELSE statements to specify default values.
Usage of Oracle DECODE function in MySQL
Oracle's DECODE function is a case statement used in Returns different values under given conditions. There is no direct equivalent to the DECODE function in MySQL. However, you can use CASE expressions to achieve similar functionality.
Syntax
<code class="sql">CASE WHEN condition1 THEN value1 WHEN condition2 THEN value2 ... ELSE default_value END</code>
Usage
Example
The following example demonstrates how to use the CASE expression to implement the DECODE function in MySQL:
<code class="sql">SELECT CASE WHEN age < 18 THEN "Minor" WHEN age >= 18 AND age < 65 THEN "Adult" ELSE "Senior" END AS age_category FROM people;</code>
Result:
age_category |
---|
Note:
CASE expression support in MySQL Use multiple WHEN clauses as well as ELSE clauses. Therefore, you can add additional conditions as needed.The above is the detailed content of How to use oracle decode function in mysql. For more information, please follow other related articles on the PHP Chinese website!