10.日期的显示格式
select hiredate from emp;
默认的日期格式是 DD-MON-RR
修改会话参数
alter system set nls_date_format='YYYY-MM-DD HH24:MI:SS';
alter session set nls_date_format='YYYY-MM-DD';
SCOTT@orcl> select hiredate from emp;
HIREDATE
----------
1980-12-17
1981-02-20
1981-02-22
1981-04-02
1981-09-28
1981-05-01
1981-06-09
1987-04-19
1981-11-17
1981-09-08
1987-05-23
1981-12-03
1981-12-03
1982-01-23
14 rows selected.
11. 虚表
虚表 Oracle自动创建的一张表 可以理解为是一张假想表 目的是为了使select语义完成
借助虚表 我们可以完成很多事
查看当前用户
select user from dual;
查看当前时间(OS时间)
select sysdate from dual;
计算
select 9+8 from dual;
获取随机数
select dbms_random.random from dual;
等等...以后还有很多查询需要借助虚表完成.
12.
过滤条件
select empno,ename,sal from emp where sal >= 1500;
WHERE是关键字 后面跟限制条件
条件由列名,字符串,算术表达式,常量和比较运算符组成;
比较运算符:
> >= < <= = <> !=
逻辑运算符:
and or not
oracle提供的比较运算符
between and
in
like
is null
数值比较 直接写数值
SQL> select ename,sal from emp where sal >= 800 and sal <= 1100;
ENAME SAL
---------- ----------
SMITH 800
ADAMS 1100
JAMES 950
SQL>
SQL> select ename,sal from emp where sal between 800 and 1100;
ENAME SAL
---------- ----------
SMITH 800
ADAMS 1100
JAMES 950
SQL>
字符串比较 字符串要加单引号 默认存储模式就是大写模式
SQL> select ename,sal from emp where ename='KING';
ENAME SAL
---------- ----------
KING 5000
SQL>
时间格式比较
日期区间表示法
先修改日期显示为我们习惯的格式 否则写系统默认的时间格式 DD-MON-RR
alter session set NLS_date_format='YYYY-MM-DD HH24:MI:SS';
select ename,hiredate from emp where hiredate between '1981-01-01' and '1981-05-31';
13.
oracle的与and 或or 非not
and(与)
select ename,sal from emp where sal<=1300 and sal >=900;
or(或)
select ename,sal from emp where sal<=1000 or sal >=4000;
not(非)
select ename,sal from emp where not sal >=4000;
与 ==> 两端都为真 返回真 若有一端或两端为假则为假
或 ==> 若有一端或两端为真则为真 两端都为假 返回假
但oracle中又引入了NULL
null不等于null
AND运算表
T and F = F T and T = T T and NULL is NULL
F and F = F F and T = F F and NULL = F
NULL and F = F NULL and T is NULL NULL and NULL is NULL
idle> select ename,sal from emp where sal >2000 and sal <3000;
ENAME SAL
---------- ----------
JONES 2975
BLAKE 2850
CLARK 2450
idle>
OR运算表
T or T = T T or F = T T or NULL = T
F or T = T F or F = F F or NULL IS NULL
NULL or T = T NULL OR F is NULL NULL OR NULL IS NULL
idle> select ename,sal,comm from emp where sal = 1250 or comm is not null;
ENAME SAL COMM
---------- ---------- ----------
ALLEN 1600 300
WARD 1250 500
MARTIN 1250 1400
TURNER 1500 0
idle>
select ename,sal,comm
from emp
where sal=1250
or
comm is null
ENAME SAL COMM
---------- ---------- ----------
SMITH 800
WARD 1250 500
JONES 2975
MARTIN 1250 1400
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
12 rows selected.
select ename,sal,comm
from emp
where sal=1250
or
comm is not null;
ENAME SAL COMM
---------- ---------- ----------
ALLEN 1600 300
WARD 1250 500
MARTIN 1250 1400
TURNER 1500 0
