Home > Article > Daily Programming > How to implement decode in mysql
The decode function in MySQL can convert the input value into a specified value based on specified conditions. Syntax: DECODE(expr, value1, result1, value2, result2, ..., valueN, resultN, default). The function checks from left to right whether the condition value matches the input value. If it matches, it returns the corresponding result. If it does not match, it returns the default value.
Implementation of decode in MySQL
decode function
The decode function is used in MySQL to convert an input value into a specified value, based on whether the input value meets specified conditions. The syntax is as follows:
<code>DECODE(expr, value1, result1, value2, result2, ..., valueN, resultN, default)</code>
Parameter description:
Working principle:
The decode function checks whether the condition value matches the expr value one by one from left to right. If there is a match, the result associated with the condition value is returned. If no match is found, the default value is returned.
Example:
<code>SELECT DECODE(gender, 'M', 'Male', 'F', 'Female', 'Unknown');</code>
This example converts the value of the gender field to human-readable text:
Note:
The above is the detailed content of How to implement decode in mysql. For more information, please follow other related articles on the PHP Chinese website!