How do you return 0 instead of null when running the following command:
在运行以下命令时,如何返回0而不是null:
SELECT MAX(X) AS MaxX
FROM tbl
WHERE XID = 1
(Assuming there is no row where XID=1)
(假设没有行,XID=1)
53
In SQL 2005 / 2008:
在SQL 2005 / 2008:
SELECT ISNULL(MAX(X), 0) AS MaxX
FROM tbl WHERE XID = 1
82
or:
或者:
SELECT coalesce(MAX(X), 0) AS MaxX
FROM tbl
WHERE XID = 1
23
Like this (for MySQL):
像这样(MySQL):
SELECT IFNULL(MAX(X), 0) AS MaxX
FROM tbl
WHERE XID = 1
For MSSQL replace IFNULL
with ISNULL
or for Oracle use NVL
对于MSSQL,将IFNULL替换为ISNULL或Oracle使用NVL。
9
You can also use COALESCE ( expression [ ,...n ] ) - returns first non-null like:
您还可以使用合并(表达式[,…)-返回第一个非空值:
SELECT COALESCE(MAX(X),0) AS MaxX
FROM tbl
WHERE XID = 1
4
Oracle would be
甲骨文将
SELECT NVL(MAX(X), 0) AS MaxX
FROM tbl
WHERE XID = 1;
3
For OLEDB you can use this query:
对于OLEDB,您可以使用此查询:
select IIF(MAX(faculty_id) IS NULL,0,MAX(faculty_id)) AS max_faculty_id from faculties;
As IFNULL is not working there
因为IFNULL不在那里工作。
2
Depends on what product you're using, but most support something like
取决于你使用的是什么产品,但大多数支持类似的东西。
SELECT IFNULL(MAX(X), 0, MAX(X)) AS MaxX FROM tbl WHERE XID = 1
or
或
SELECT CASE MAX(X) WHEN NULL THEN 0 ELSE MAX(X) FROM tbl WHERE XID = 1
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2009/11/06/d3580aa2428ba3015083e21a8abb7521.html。