1. nvl()函数使用
- 语法:NVL(expr1,expr2)
如果oracle第一个参数为空那么显示第二个参数的值,如果第一个参数的值不为空,则显示第一个参数本来的值
--从学生表中查询学生的职务,如果该列上的值为空值则结果显示“没有职务”。
select s.s_name,nvl(s.S_DUTY,'没有职务') FROM STUDENT s
2. 列前加字符常量的方法
- 语法:‘str’|| 原字段
- 从学生表中查询学生的学号和姓名,并在学号和姓名的前面分别加上字符常量“学生学号”和“学生姓名”。
SELECT '学生学号:'|| s.S_ID,'学生姓名:'||s.S_NAME FROM STUDENT s ;
3. between … and … 在日期格式中的使用
注意右边界问题
--查询入职时间在2003年4月1日到2003年4月30日之间的教师的相关信息。
SELECT t.* from TEACHER t where t.T_ENTERTIME BETWEEN to_date('2003_04_01','yyyy_mm_dd') and to_date('2003_04_30 14:23:45','yyyy_mm_dd HH24:MI:SS');
-- 注意:该查询语句那边默认右边界位 2003_04_30 00:00:00
SELECT t.* from TEACHER t where to_char(t.T_ENTERTIME,'yyyy_mm') like '2003_04%';
4. to_date() 字符串转日期函数
日期格式的数据可以进行比大小
--查询入职时间在2003年4月1日到2003年4月30日之间的教师的相关信息。
SELECT t.* from TEACHER t where t.T_ENTERTIME BETWEEN to_date('2003_04_01','yyyy_mm_dd') and to_date('2003_04_30','yyyy_mm_dd');
5. to_char()日期转字符串函数
--查询入职时间在2003年4月的教师的相关信息。
SELECT t.* from TEACHER t where to_char(t.T_ENTERTIME,'yyyy_mm') like '2003_04%';
6. 多表连接与where、and 的使用
-- table1 left join table2 on ... where 条件
-- 先左连接后where后面条件筛选
SELECT * from dept LEFT JOIN emp on dept.deptno=emp.deptno WHERE dept.deptno=10;
SELECT * from dept LEFT JOIN emp on dept.deptno=emp.deptno WHERE (dept.deptno=10 and emp.ename='king');
-- table1 left join table2 on ... and 条件
-- 先对右表table2进行and条件筛选然后左连接
SELECT * from dept LEFT JOIN emp on dept.deptno=emp.deptno and emp.deptno=10;
SELECT * from dept LEFT JOIN emp on dept.deptno=emp.deptno and (emp.deptno=10 and emp.ename='king');
7. case when then else end
简单case函数
case column
when 'A' then 'a'
when 'B' then 'b'
else 0 end;
搜索case函数
case when column='A' then 'a'
when column='B' then 'b'
else 0 end;