oracle 基础

时间:2023-03-08 23:35:06
oracle 基础

1.order by 排序

select * from dept order by desc; --降序

select ename,empno from emp order by empno asc;  --对 empno升序排列

  select ename,empno from emp where deptno<>10 order by empno asc;-- deptno进行升序排列同时过滤deptno=10的数据

select ename,sal,deptno from emp order by deptno asc,ename desc;  --deptno进行升序排列的前提下,ename进行降序排列

2.去重

<1> distinct

distinct关键字只能过滤查询字段中所有记录相同的(记录集相同),而如果要指定一个字段却没有效果,另外distinct关键字会排序,效率很低 。

例:

      select distinct name from t1 能消除重复记录,但只能取一个字段,现在要同时取id,name这2个字段的值。

       select distinct id,name from t1 可以取多个字段,但只能消除这2个字段值全部相同的记录

<2>group by

例如要显示的字段为A、B、C三个,而A字段的内容不能重复可以用下面的语句:

select A, min(B),min(C),count(*) from [table] where [条件] group by   A  having [条件] order by A desc   -- count(*)  重复次数

如果在上句中having加 count(*)>1   就可以查出记录A的重复次数大于1的记录

    如果在上句中having加 count(*)>2   就可以查出记录A的重复次数大于2的记录

3. group by 字句 分组查询

select deptno ,sum(sal) from emp group by deptno;   --统计各个部门的员工的工资的总和