ITeye博客

使用 IFNULL 或 COALESCE

dewei · 2013-03-07 19:06 · 数据库 · 数据库 · Markdown

本文转自:http://stackoverflow.com/questions/7602271/how-do-i-get-sum-function-in-mysql-to-return-0-if-no-values-are-found

 Use IFNULL or COALESCE:

SELECT IFNULL(SUM(Column1), 0) AS total FROM...

SELECT COALESCE(SUM(Column1), 0) AS total FROM...

 The difference between them is that IFNULL is a MySQL extension that takes two arguments, and COALESCE is a standard SQL function that can take one or more arguments. When you only have two arguments using IFNULL is slightly faster, though here the difference is insignificant since it is only called once.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。