ORACLE之distinct,||,decode,with as,trunc(),union和union all

1.1. ORACLE去重函数distinct
表1:
在这里插入图片描述
表2:
在这里插入图片描述
eg:

select distinct name from A`

执行结果:
在这里插入图片描述

select distinct name, id from A

执行结果:
在这里插入图片描述
2.||符号是字符串拼接
比如:
‘abc’ || ‘def’ 结果为:‘abcdef’
3.decode用法
decode(条件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值)
该函数的含义如下:
IF 条件=值1 THEN
    RETURN(翻译值1)
ELSIF 条件=值2 THEN
    RETURN(翻译值2)
    …
ELSIF 条件=值n THEN
    RETURN(翻译值n)
ELSE
    RETURN(缺省值)
END IF

decode(字段或字段的运算,值1,值2,值3)
这个函数运行的结果是,当字段或字段的运算的值等于值1时,该函数返回值2,否则返回值3
当然值1,值2,值3也可以是表达式,这个函数使得某些sql语句简单了许多
例子:

select decode(dir,1,0,1) from a1_interval

dir 的值是1变为0,是0则变为1
select count() from 表 where 性别 = 男;
select count(
) from 表 where 性别 = 女;
要想显示到一起还要union一下,太麻烦了
用decode呢,只需要一句话
select sum(decode(性别,男,1,0)),sum(decode(性别,女,1,0)) from 表

select sum(decode(siteno,'LT',1,0)),sum(decode(siteno,'SZ',1,0)) from facd605;
select sum(case siteno when 'LT' then 1 else 0 end),sum(case siteno when 'SZ' then 1 else 0 end) from facd605;

例子:

select *from users;
在这里插入图片描述
select id, username, age, decode(sex,0,‘男’,1,‘女’) from users;
在这里插入图片描述

4.with as 取别名
–针对一个别名
with tmp as (select * from tb_name)
–针对多个别名
with
tmp as (select * from tb_name),
tmp2 as (select * from tb_name2),
tmp3 as (select * from tb_name3),

–相当于建了个e临时表
with e as (select * from scott.emp e where e.empno=7499)
select * from e;

–相当于建了e、d临时表
with
e as (select * from scott.emp),
d as (select * from scott.dept)
select * from e, d where e.deptno = d.deptno;

5.trunc()
Oracle中的trunc()函数是对时间类型或者数字进行截取操作的。
trunc(Date)的用法一般有以下几种:
select sysdate from dual
select to_char(sysdate,‘yyyy-mm-dd hh:mi:ss’) from dual
select trunc(sysdate,‘yyyy’) from dual --当年的第一天
select trunc(sysdate,‘mm’) from dual --当月的第一天
select trunc(sysdate,‘dd’) from dual --当前时间(精确到天)
select trunc(sysdate,‘d’) from dual --当前星期的第一天
select trunc(sysdate,‘hh’) from dual --当前时间(精确到小时)
select trunc(sysdate,‘mi’) from dual --当前时间(精确到分钟,没有精确到秒的)
trunc(number)的用法一般有以下几种:
trunc(55.5,-1) = 50; //-1(负数)表示从小数点左边第一位截取后面全置为零;
trunc(55.55,1) = 55.5; //1(正数)表示小数点后面保留一位;
trunc(55.55) = 55; //截取整数部分;

6.union和union all
Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
Union All:对两个结果集进行并集操作,包括重复行,不进行排序;
eg:

drop table student;

create table student

(

id int primary key,

name nvarchar2(50) not null,

score number not null

);

insert into student values(1,'Aaron',78);

insert into student values(2,'Bill',76);

insert into student values(3,'Cindy',89);

insert into student values(4,'Damon',90);

insert into student values(5,'Ella',73);

insert into student values(6,'Frado',61);

insert into student values(7,'Gill',99);

insert into student values(8,'Hellen',56);

insert into student values(9,'Ivan',93);

insert into student values(10,'Jay',90);

commit;

测试 union 命令的结果集,sql语句如下:

select *

from student

where id < 4

union

select *

from student

where id > 2 and id < 6

结果将是:
      1 Aaron 78
      2 Bill 76
      3 Cindy 89
      4 Damon 90
      5 Ella 73
如果将union换成union all连接两个结果集,则返回结果是:
1 Aaron 78
    2 Bill 76
    3 Cindy 89
    3 Cindy 89
    4 Damon 90
    5 Ella 73

发布了49 篇原创文章 · 获赞 1 · 访问量 5551

猜你喜欢

转载自blog.csdn.net/weixin_44999591/article/details/105347994