oracle decode function explain

来源:互联网 发布:js监听ios软键盘事件 编辑:程序博客网 时间:2024/06/07 13:12

我们往往在做和并行的时候会用到oracle的decode()函数,他的使用格式为:


它的含义是,当expr的值和search值相同的时候就显示result,否则显示default值。详情可参考:oracle decode

举例说明: 

我们这里有三张表,分别是:分数表、学生表、课程表。

分数表:

-- Create tablecreate table mzsf.TSCORE(  fid      NUMBER(5) not null,  fstuid   NUMBER(5),  fclassid NUMBER(5),  fscore   NUMBER(3))tablespace MZSF  pctfree 10  initrans 1  maxtrans 255  storage  (    initial 64K    next 1M    minextents 1    maxextents unlimited  );

学生表:

-- Create tablecreate table mzsf.TSTUDENT(  fid   NUMBER(5) not null,  fname VARCHAR2(10),  fage  NUMBER(2))tablespace MZSF  pctfree 10  initrans 1  maxtrans 255  storage  (    initial 64K    next 1M    minextents 1    maxextents unlimited  );

课程表:

-- Create tablecreate table mzsf.TCLASS(  fid        NUMBER(5) not null,  fclassname VARCHAR2(10))tablespace MZSF  pctfree 10  initrans 1  maxtrans 255  storage  (    initial 64K    next 1M    minextents 1    maxextents unlimited  );

我们根据他们之间的关联关系查询的结果为:


虽然我们查询出了张三和李四各科的成绩,但是看起来不显得那么明了,如果我们能够将李四的各科成绩都合并成一行来显示,那给人的感觉就一目了然,这里我们学到的decode函数将派上用场:


这样看起来就清晰很多了


0 0
原创粉丝点击