Summary of coalesce() usage tips in MySQL

Summary of coalesce() usage tips in MySQL

Preface

Recently, I accidentally discovered MySQL's coalesce. I just had time, so I summarized the usage tips of coalesce() in MySQL and shared them with you. Let's take a look at the detailed introduction:

coalesce() explained

Returns the first non-empty expression in the argument (from left to right, and so on);

Usage Examples

There are three variables: a, b, and c.

select coalesce(null,2,3); // Return 2

select coalesce(null,null,3); // Return 3

select coalesce(1,2,3); // Return 1

As can be seen from the above example, its function is to return the first non-null value in the passed parameter.

SELECT COALESCE(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1); 
-- Return 1

If all the passed parameters are null, null is returned, for example

SELECT COALESCE(NULL, NULL, NULL, NULL); 
-- Return NULL

This parameter is used when: if a field is null by default, and you want it to return something other than null, such as 0 or other values, you can use this function

SELECT COALESCE(field name,0) as value from t; // (If the default value of the database commission field is not 0, it must be the developer's fault. Please don't criticize my personal opinion).

Summarize

The above is the full content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Summary of knowledge points related to null (IFNULL, COALESCE and NULLIF) in MySQL
  • Detailed explanation of IFNULL() and COALESCE() functions to replace null in MySQL
  • SQL Server COALESCE Function Detailed Explanation and Examples
  • A brief analysis of the performance comparison between the paging modes ISNULL and COALESCE in SQL Server
  • MySQL time difference functions (TIMESTAMPDIFF, DATEDIFF), date conversion calculation functions (date_add, day, date_format, str_to_date)
  • MySQL uses custom functions to recursively query parent ID or child ID
  • Detailed explanation of the correct use of the if function in MySQL
  • MySql COALESCE function usage code example

<<:  Docker realizes the connection with the same IP network segment

>>:  Swiper.js plugin makes it super easy to implement carousel images

Recommend

3D tunnel effect implemented by CSS3

The effect achievedImplementation Code html <d...

How to install Postgres 12 + pgadmin in local Docker (support Apple M1)

Table of contents introduce Support Intel CPU Sup...

Detailed explanation of the Sidecar mode in Docker Compose

Table of contents What is Docker Compose Requirem...

Detailed explanation of how Angular handles unexpected exception errors

Written in front No matter how well the code is w...

MySQL automatically inserts millions of simulated data operation code

I use Navicat as my database tool. Others are sim...

Mybatis mysql delete in operation can only delete the first data method

Bugs As shown in the figure, I started to copy th...

Flex layout realizes left text overflow and omits right text adaptation

I want to achieve a situation where the width of ...

Several CSS3 tag shorthands (recommended)

border-radius: CSS3 rounded corners Syntax: borde...

HTML table markup tutorial (48): CSS modified table

<br />Now let's take a look at how to cl...

Docker container monitoring and log management implementation process analysis

When the scale of Docker deployment becomes large...