唱着那无人问津的歌谣~如果有一天,我老无所依~请把我留在那时光里~
题目:
答案解释:exhibit是一个很多表的图,不贴出来了,题意简单就是检验会不会用count及nvl了,看例子吧
SQL> select * from t1;
ID NAME
--------------------------------------- ----------
1 john
2
3
4 jash
2 kode
SQL> select count(*) from t1; 统计有多少记录
COUNT(*)
----------
5
SQL> select count(id) from t1; 统计id有多少值,排除NULL
COUNT(ID)
----------
5
SQL> select count(name) from t1; 统计name有多少值,排除NULL
COUNT(NAME)
-----------
3
SQL> select count(distinct id) from t1; 统计id有多少值,排除重复值及NULL
COUNT(DISTINCTID)
-----------------
4
SQL> select count(distinct name) from t1; 统计name有多少值,排除重复值及NULL
COUNT(DISTINCTNAME)
-------------------
3
SQL> select count(name) from t1 where name is null; 因为count不统计null值,所以为0
COUNT(NAME)
-----------
0
SQL> select count(name) from t1 where name = null; 因为count不统计null值,所以为0
COUNT(NAME)
-----------
0
SQL> select nvl(name, 'nobody') from t1; 如果name栏位值是NULL,则返回nobody字符
NVL(NAME,'NOBODY')
------------------
john
nobody
nobody
jash
kode
SQL> select count(nvl(name, 'nobody')) from t1; 统计name有多少值(由于NULL记录被nobody替换了)
COUNT(NVL(NAME,'NOBODY'))
-------------------------
5
SQL>
通过例子,count的作用就不说了,常见的就像例子的用法。下面说下NVL这个函数,下面的文字来源于百度百科:
NVL的概念:
Oracle/PLSQL中的一个函数。
格式为:NVL( string1, replace_with)
功能:如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值,如果两个参数的都为NULL ,则返回NULL。
注意事项:string1和replace_with必须为同一数据类型,除非显式的使用TO_CHAR函数进行类型转换。
例:NVL(TO_CHAR(numeric_column), 'some string') 其中numeric_column代指某个数字类型的值。
例:nvl(yanlei777,0) > 0 NVL(yanlei777, 0) 的意思是 如果 yanlei777 是NULL, 则取 0值
通过查询获得某个字段的合计值,如果这个值为null将给出一个预设的默认值
例如:select nvl(sum(t.dwxhl),1) from tb_jhde t 就表示如果sum(t.dwxhl) = NULL 就返回 1