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:
- expr: The expression to be evaluated.
- value1, value2, ..., valueN: The condition value to be checked.
- result1, result2, ..., resultN: Return result when matching the corresponding condition value.
- default: The default result when the input value does not match any condition value.
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:
- If the gender value is ' M', then return 'Male'.
- If the gender value is 'F', return 'Female'.
- If the gender value is not 'M' or 'F', 'Unknown' is returned.
Note:
- decode function is case-sensitive.
- The default value is optional. If not provided, the function returns NULL when expr does not match any condition value.
- The decode function can handle multiple conditions, and up to 127 condition values and results can be specified at the same time.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Atom editor mac version download
The most popular open source editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
