MySQL COALESCE function

Source: Internet
Author: User

The COALESCE function returns the first non-null value from a list of values and replaces it with 0 when it encounters a null value. COALESCE (Str1,str2 ...);

e.g. All employees who are lower than the ' WARD ' Commission (COMM) are required to be identified in the table, and the employee who made the null is included. (Personal opinion, if the default value of the Database Commission field is not a value of 0, it is certainly the fault of developing that).

Select Ename,comm from emp where COALESCE (comm,0) < (select comm from emp whrer ename = "WARD");

Results:

+--------+------+| ename  | Comm |+--------+------+| SMITH  | NULL | | ALLEN  |  300 | | JONES  | NULL | | BLAKE  | NULL | | CLARK  | NULL | | SCOTT  | NULL | | KING   | NULL | | TURNER |    0 | | JAMES  | NULL | | MILLER | NULL | | ADAMS  | NULL | | FORD   | NULL |+--------+------+12 rows in Set

  

Returns a non-null value:

Select Ename, Comm,coalesce (comm,0) from the EMP where COALESCE (comm,0) < (select comm from emp where ename = ' WARD ');

  

+--------+------+------------------+| ename  | comm | COALESCE (comm,0) |+--------+------+------------------+| SMITH  | NULL |                0 | | ALLEN  |  |              300 | | JONES  | NULL |                0 | | BLAKE  | NULL |                0 | | CLARK  | NULL |                0 | | SCOTT  | NULL |                0 | | KING   | NULL |                0 | | TURNER |    0 |                0 | | JAMES  | NULL |                0 | | MILLER | NULL |                0 | | ADAMS  | NULL |                0 | | FORD   | NULL |                0 |+--------+------+------------------+12 rows in Set

  

MySQL COALESCE function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.