Summary of using skills of coalesce and of in mysql

  • 2021-08-12 03:53:46
  • OfStack

Preface

Recently, I accidentally discovered coalesce of mysql, and just had time, so I put mysql coalesce() I will share with you the summary of the use skills. Let's take a look at the detailed introduction:

coalesce () interpretation

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

Use sample

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, his function is to return the first non-null value in the passed-in parameter, and another example is


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

If all the parameters passed in are null, null is returned, such as


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

If a field defaults to null and you want it to return a value such as 0 or something else instead of null, 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 If it is worth it, it must be the fault of developing it. Please don't spray your personal opinion. 

Summarize


Related articles: