mysql coalesce函數

來源:互聯網
上載者:User

標籤:select   員工   函數   cot   替換   oal   預設值   pre   ams   

 COALESCE函數從值列表中返回第一個非NULL的值,當遇到NULL值時將其替換為0。 coalesce(str1,str2....);

e.g.   需要在表中查出所有比‘WARD‘提成(COMM)低的員工,提成為NULL的員工也包括在內。 (個人意見,如果資料庫提成欄位預設值不是為0值的話肯定是開發那個的錯)。

 

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

  結果:

+--------+------+| 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

  

返回非NULL值:

select ename, comm,coalesce(comm,0) from emp where coalesce(comm,0) < (select comm from emp where ename = ‘WARD‘);

  

+--------+------+------------------+| ename  | comm | coalesce(comm,0) |+--------+------+------------------+| SMITH  | NULL |                0 || ALLEN  |  300 |              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函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.