1

我得到了一个正确的结果,但现在不是向我显示结果 0 我希望他们向我显示 null 结果。我怎样才能得到结果 null 而不是 0 ?

SELECT w.FIRST_NAME,w.LAST_NAME,COUNT(s.SECTION_ID) AS COUNTED_SECTIONS
FROM INSTRUCTOR w LEFT OUTER JOIN SECTION s 
ON w.INSTRUCTOR_ID = s.INSTRUCTOR_ID
GROUP BY w.FIRST_NAME,w.LAST_NAME
ORDER BY w.LAST_NAME;

当前显示

FIRST_NAME                LAST_NAME                 COUNTED_SECTIONS
------------------------- ------------------------- ----------------
Rick                      Chow                                     0 
Marilyn                   Frantzen                                10 
Fernand                   Hanks                                    9 
Charles                   Lowry                                    0
etc

但我想要

FIRST_NAME                LAST_NAME                 COUNTED_SECTIONS
------------------------- ------------------------- ----------------
Rick                      Chow                                      
Marilyn                   Frantzen                                10 
Fernand                   Hanks                                    9 
Charles                   Lowry                                    
etc

我用 NVL 试过了,还是不行

NVL(COUNT(s.SECTION_ID),NULL) AS COUNTED_SECTIONS
4

3 回答 3

2

我认为NULLIF()在 oracle 中可用:

SELECT w.FIRST_NAME,w.LAST_NAME,NULLIF(COUNT(s.SECTION_ID),0) AS COUNTED_SECTIONS
FROM INSTRUCTOR w LEFT OUTER JOIN SECTION s 
ON w.INSTRUCTOR_ID = s.INSTRUCTOR_ID
GROUP BY w.FIRST_NAME,w.LAST_NAME
ORDER BY w.LAST_NAME;
于 2013-11-08T22:22:37.777 回答
1

尝试 nullif:

SELECT w.FIRST_NAME,w.LAST_NAME, NULLIF(COUNT(s.SECTION_ID), 0) AS COUNTED_SECTIONS
FROM INSTRUCTOR w LEFT OUTER JOIN SECTION s 
ON w.INSTRUCTOR_ID = s.INSTRUCTOR_ID
GROUP BY w.FIRST_NAME,w.LAST_NAME
ORDER BY w.LAST_NAME;
于 2013-11-08T22:25:02.767 回答
1

您可以使用DECODE来决定要显示的内容

DECODE(COUNT(s.SECTION_ID),0, NULL, COUNT(s.SECTION_ID)) AS COUNTED_SECTIONS
于 2013-11-08T22:25:44.667 回答