以ORACLE数据库 scott用户的emp 表为例
原始数据如下:
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
7566 JONES MANAGER 7839 1981-4-2 2975.00 20
7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30
7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30
7782 CLARK MANAGER 7839 1981-6-9 2450.00 10
7788 SCOTT ANALYST 7566 1987-4-19 3000.00 20
7839 KING PRESIDENT 1981-11-17 5000.00 10
7844 TURNER SALESMAN 7698 1981-9-8 1500.00 0.00 30
7876 ADAMS CLERK 7788 1987-5-23 1100.00 20
7900 JAMES CLERK 7698 1981-12-3 950.00 30
7902 FORD ANALYST 7566 1981-12-3 3000.00 20
7934 MILLER CLERK 7782 1982-1-23 1300.00 10
14 rows selected
为了测试,插入两条测试记录:
SQL> insert into emp(empno,ename) values(9999,'XIAOYANZI');
1 row inserted
SQL> insert into emp(empno,ename) values(8888,'HUANGPEI');
1 row inserted
插入记录后的结果:
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
7566 JONES MANAGER 7839 1981-4-2 2975.00 20
7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30
7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30
7782 CLARK MANAGER 7839 1981-6-9 2450.00 10
7788 SCOTT ANALYST 7566 1987-4-19 3000.00 20
7839 KING PRESIDENT 1981-11-17 5000.00 10
7844 TURNER SALESMAN 7698 1981-9-8 1500.00 0.00 30
7876 ADAMS CLERK 7788 1987-5-23 1100.00 20
7900 JAMES CLERK 7698 1981-12-3 950.00 30
7902 FORD ANALYST 7566 1981-12-3 3000.00 20
7934 MILLER CLERK 7782 1982-1-23 1300.00 10
9999 XIAOYANZI 空值
8888 HUANGPEI 空值
16 rows selected
COUNT
不保留空值,保留重复值。
(针对空值处理,去掉空值)
SQL> select COUNT(sal) from emp;
COUNT(SAL)
----------
14
DISTINCT
保留空值,所有重复空值合并,所有重复值合并。
(针对重复值处理,合并重复值)
SQL> select DISTINCT sal from emp;
SAL
---------
800.00
950.00
1100.00
1250.00
1300.00
1500.00
1600.00
2450.00
2850.00
2975.00
3000.00
5000.00
空值
13 rows selected
COUNT+DISTINCT
不保留空值,所有重复值合并。
(针对空值处理,去掉空值;针对重复值处理,合并重复值)
SQL> select COUNT(DISTINCT sal) from emp;
COUNT(DISTINCTSAL)
------------------
12
show user; 显示当前登录用户
disconnect; 断开连接
connect sys/sys as sysdba; SYS用户登录