cookbook学习笔记一

select * from scott.dept;
#检索所有行
select * from scott.emp;
#检索部分行
select * from scott.emp where deptno=10;
#查找满足条件的行
select * from scott.emp where( deptno=10 or comm is not null or sal<=2000) and deptno=20;
#检索部分列
select ename,deptno,sal from scott.emp;
#为列取有意义的名称
select sal as salary,comm as commission from scott.emp;
#在where子句中引用别名的列
select sal as salary,comm as commission from scott.emp where salary<5000;
select * from (select sal as salary,comm as commission from scott.emp) x where salary<5000;
#连接列值
select ename,job from scott.emp where deptno=10;
select ename || 'WORKS AS A '||job as msg from scott.emp where deptno=10;
#在select中使用条件逻辑
select ename,sal,case
when sal<=2000 then 'UNDRERPAID'
when sal>=400 then 'OVERPAIN'
else 'OK'
end as status
from scott.emp;
#限制返回的行数
select * from scott.emp where rownum<=5;
#从表中随机返回n条记录
select * from ( select ename,job from scott.emp order by dbms_random.value) where rownum<=5;
#查找空值
select * from scott.emp where comm is null;
#将空值转换为实际值
select coalesce(comm,0) from scott.emp;
#按模式搜索
select ename,job,deptno from scott.emp where deptno in(10,20);
select ename,job,deptno from scott.emp where deptno in(10,20) and (ename like '%I%' or job like '%ER');
#查询结果排序
#以指定的次序返回查询结果
select ename,job,sal from scott.emp where deptno=10 order by sal asc;
select ename,job,sal from scott.emp where deptno=10 order by sal desc;
#按多个字段排序
select * from scott.emp;
select empno,deptno,sal,ename,job from scott.emp order by deptno,sal desc;
#按子串排序
select ename,job from scott.emp order by substr(job,length(job)-2);
#对字母数字混合的数据排序(视图)
create view v as select ename||' in '||deptno as data from scott.emp;
select * from v;
#按deptno排序
select data from v order by replace(data,replace(translate(data,'0123456789','##########'),'#',''),'');
#按ename排序
select data from v order by replace(translate(data,'0123456789','##########'),'#','');
#处理排序空值
select ename,sal,comm from scott.emp order by 3;
select ename,sal,comm from scott.emp order by 3 desc;
#根据数据项的键排序
select ename,sal,job,comm from scott.emp order by case
when job='SALESMAN' then comm
else sal end;
select * from scott.emp;
select * from scott.dept;
#记录集的叠加(多个表的行组合在一起)
select ename as ename_and_dname, deptno from scott.emp
where deptno=10
union all
select '-------------------',null
from scott.dept where rownum=1
union all
select dname,deptno
from scott.dept;
#组合相关的行
select e.ename,d.loc
from scott.emp e,scott.dept d
where e.deptno=d.deptno and e.deptno=10;
#两个表中查找共同行
create view v1
as select ename,job,sal from scott.emp
where job='CLERK'
#并没有返回所有列
select * from v1;
#返回所有列
select empno,ename,job,sal,deptno from scott.emp
where (ename,job,sal)
in (select ename,job,sal from scott.emp
intersect select ename,job,sal from v1);
#从一个表中查找另一个表中没有的值
select deptno from scott.dept minus select deptno from scott.emp;
#在一个表中查找与其他表不匹配的记录
select d.* from scott.dept d,scott.emp e
where d.deptno=e.deptno(+)
and e.deptno is not null;
#向查询中增加联接而不影响其他联接
select e.ename,d.loc,b.received from scott.enp e,scott.dept d,scott.bonus b
where e.deptno=d.deptno
and e.empno=b.empno(+)
order by 2;
#检测两个表中是否有相同的数据
create view v2
as select * from scott.emp where deptno!=10
union all
select * from scott.emp where ename='WARD'
select * from v2;

(select empno,ename,job,mgr,hiredate,sal,comm,deptno,count(*) as cnt
from v2
group by empno,ename,job,mgr,hiredate,sal,comm,deptno
minus
select empno,ename,job,mgr,hiredate,sal,comm,deptno,count(*) as cnt
from scott.emp
group by empno,ename,job,mgr,hiredate,sal,comm,deptno)
union all
(select empno,ename,job,mgr,hiredate,sal,comm,deptno,count(*) as cnt
from scott.emp
group by empno,ename,job,mgr,hiredate,sal,comm,deptno
minus
select empno,ename,job,mgr,hiredate,sal,comm,deptno,count(*) as cnt
from v2
group by empno,ename,job,mgr,hiredate,sal,comm,deptno
)
#识别和消除笛卡儿积
select e.ename,d.loc,e.deptno from scott.emp e,scott.dept d
where e.deptno=10;
select e.ename,d.loc,e.deptno from scott.emp e,scott.dept d
where e.deptno=10 and e.deptno=d.deptno;
#新建奖金表
create table scott.emp_bonus(empno varchar(4),
receiver date,
type int);
#聚集和联接
select distinct deptno,total_sal,total_bonus
from (
select e.empno,
e.ename,
sum(distinct e.sal) over
(partition by e.deptno) as total_sal,
e.deptno,
sum(e.sal*case when eb.type=1 then .1
when eb.type=2 then .2
else .3 end) over
(partition by deptno) as total_bonus
from scott.emp e,scott.emp_bonus eb
where e.empno=eb.empno
and e.deptno=10)x
#聚集与外联接
select deptno,
sum(distinct sal) as total_sal,
sum(bonus) as total_bonus
from(
select e.empno,
e.ename,
e.sal,
e.deptno,
e.sal* case when eb.type is null then 0
when eb.type=1 then .1
when eb.type=2 then .2
else .3 end as bonus
from scott.emp e , scott.emp_bonus eb
where e.empno=eb.empno(+)
and e.deptno=10)
group by deptno;

#多从个表中返回丢失的数据
select d.deptno,d.dname,e.ename from scott.dept d,scott.emp e
where d.deptno=e.deptno(+)
union
select d.deptno,d.dname,e.ename from scott.dept d,scott.emp e
where d.deptno(+)=e.deptno


#在运算比较时使用null值
select ename,comm from scott.emp where coalesce(comm,0) < (select comm from scott.emp where ename='WARD');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL 是计算机世界的语言,在用关系数据库开发报表时,将数据放入数据库以及从数据库中取出来,都需要SQL 的知识。很多人以一种马马虎虎的态度在使用SQL,根本没有意识到自己掌握着多么强大的武器。本书的目的是打开读者的视野,看看SQL 究竟能干什么,以改变这种状况。, 本书是一本指南,其中包含了一系列SQL 的常用问题以及它们的解决方案,希望能对读者的日常工作有所帮助。本书将相关主题的小节归成章,如果读者遇到不能解决的SQL 新问题,可以先找到最可能适用的章,浏览其中各小节的标题,希望读者能从中找到解决方案,至少可以找到点灵感。, 在这本书中有150 多个小节,这还仅仅是SQL 所能做的事情的一鳞半爪。解决日常编程问题的解决方案的数量仅取决于需要解决的问题的数量,本书没有覆盖所有问题,事实上也不可能覆盖;然而从中可以找到许多共同的问题及其解决方案,这些解决方案中用到许多技巧,读者学到这些技巧就可以将它们扩展并应用到本书不可能覆盖的其他新问题上。, 毫无疑问,本书的目标是让读者看到,SQL 能够做多少一般认为是SQL 问题范围之外的事情。在过去的10 年间,SQL 走过了很长的路,许多过去只能用C 和JAVA等过程化语言解决的典型问题现在都可以直接用SQL 解决了,但是很多开发人员并没有意识到这一事实。本书就是要帮助大家认识到这一点。, 现在,在对我刚才的话产生误解之前我先要申明:我是“如果没坏,就别去修它”这一教义的忠实信徒。例如,假如你有一个特定的业务问题要解决,目前只用SQL检索数据,而其他复杂的业务逻辑由其他语言完成,如果代码没有问题,而且性能也过得去,那么,谢天谢地。我绝对无意建议你扔掉以前的代码重新寻求完全SQL 的解决方案;我只是请你敞开思想,认识到1995 年编程用的SQL 跟2005 年用的不是一回事,今天的SQL 能做的事要多得多。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值