Oracle数据库函数<一>
nvl(COMMISSION_PCT,0) 判定,若该值为null则变为0
SELECT last_name||job_id AS "Employees"FROM employees; 链接两个行内容
SELECT last_name ||' is a '||job_id AS "Employee Details"
FROM employees;
如: King is a AD_PRES
SELECT DISTINCT department_id, job_id
FROM employees; distinct 消除重复
SELECT employee_id, manager_id, department_id
FROM employees
WHERE last_name IN ('Hartstein', 'Vargas'); in 判断是否在该范围
between int and int;
条件
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%'; 使用like 比局部相等
% 代表任意个字符
_ 代表一个任意字符
SELECT last_name, manager_id
FROM employees
WHERE manager_id IS NULL; null判断是否为空
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >=10000 优先级 and > or > not
AND job_id LIKE '%MAN%'; and 两个都为真为真
not 条件为假才为真
or 一个为真则为真
对于优先级不好把握的时候建议用括号
这样就会更加直观
SELECT employee_id, last_name, salary*12 annsal
FROM employees
ORDER BY annsal; 按照别名排序
SELECT last_name, department_id, salary
FROM employees
ORDER BY department_id asc, salary DESC; 多个对象排序
后者是在前者的基础上进行排序而且不改前者的排序规则
LOWER('SQL Course') sql course
UPPER('SQL Course') SQL COURSE
INITCAP('SQL Course') Sql Course
SELECT employee_id, last_name, department_id
FROM employees
WHERE LOWER(last_name) = 'higgins'; 减少不必要的麻烦
CONCAT('Hello', 'World') HelloWorld 连接
SUBSTR('HelloWorld',1,5) Hello 抽取指定长度
LENGTH('HelloWorld') 10 显示字符长度
INSTR('HelloWorld', 'W') 6 查找指定字符的位置
LPAD(salary,10,'*') *****24000 按右对齐填充
RPAD(salary, 10, '*') 24000***** 按左对齐填充
TRIM('H' FROM 'HelloWorld' elloWorld 截取头部或尾部
数字函数
ROUND(column|expression,n) 四舍五入保留n位小数
TRUNC(column|expression,n) 截取到n个小数不舍如
MOD (column|expression,n) m除n取余
默认日期显示格式
DD-MON-RR
SELECT SYSDATE
FROM DUAL; 返回当前系统时间
SELECT employee_id, hire_date,
MONTHS_BETWEEN (SYSDATE, hire_date) TENURE,
ADD_MONTHS (hire_date, 6) REVIEW,
NEXT_DAY (hire_date, 'FRIDAY'), LAST_DAY(hire_date
FROM employees
WHERE MONTHS_BETWEEN (SYSDATE, hire_date) < 36;
select last_name ,to_char(hire_date,'fmDdspth "of" Month YYYY fmHH:MI:SS AM')Hire_Date
from employees;
SELECT last_name,TO_CHAR(hire_date, 'fmDD Month YYYY')
AS HIREDATE
FROM employees; 使用To_char处理日期
前面的加上fm
HH24:MI:SS AM 15:45:32 PM
DD "of" MONTH 12 of OCTOBER
ddspth fourteenth
To_char(,'')
SELECT TO_CHAR(salary, '$99,999.00') SALARY
FROM employees
WHERE last_name = 'Ernst'; to_char处理数字
To_Number(char,'')
SELECT last_name, hire_date
FROM employees
WHERE hire_date = TO_DATE('May 24, 1999', 'fxMonth DD, YYYY');
To_Date(char,'')
RR日期格式
给定日期与当前日期同一范围 不同范围
0-49 返回当前 返回上一世纪
50-99 返回当前 返回下一世纪
YY日期格式
返回当前世纪
嵌套表达式
1
SELECT last_name,
NVL(TO_CHAR(manager_id), 'No Manager')
FROM employees
WHERE manager_id IS NULL;
2
SELECT TO_CHAR(NEXT_DAY(ADD_MONTHS
(hire_date, 6), 'FRIDAY'),
'fmDay, Month DDth, YYYY')
"Next 6 Month Review"
FROM employees
ORDER BY hire_date; 函数可以实现重复嵌套
NVL(to_char(x),'') 若x为null则用''内的值取代
NVL2 (expr1, expr2, expr3) 若expr1位空返回exper3
否则返回exper2
NULLIF (expr1, expr2) exper1与exper2比
若相等返回空值
否者返回exper1
CASE WHEN expr1 = expr 2 THEN NULL ELSE expr1 END
COALESCE函数表达式
SELECT last_name,
COALESCE(commission_pct, salary, 10) comm
FROM employees
ORDER BY commission_pct; 如果com不为空显示com
若com为空显示salary
若salary也为空显示10
CASE expr WHEN comparison_expr1 THEN return_expr1
[WHEN comparison_expr2 THEN return_expr2
WHEN comparison_exprn THEN return_exprn
ELSE else_expr]
END
通过IF-THen-ELSE 简化查询
SELECT last_name, job_id, salary,
CASE job_id WHEN 'IT_PROG' THEN 1.10*salary
WHEN 'ST_CLERK' THEN 1.15*salary
WHEN 'SA_REP' THEN 1.20*salary
ELSE salary END "REVISED_SALARY"
FROM employees;
DECODE解码
DECODE(col|expression, search1, result1
[, search2, result2,...,] [, default])
SELECT last_name, job_id, salary,
DECODE(job_id, 'IT_PROG', 1.10*salary,
'ST_CLERK', 1.15*salary,
'SA_REP', 1.20*salary,salary)REVISED_SALARY
FROM employees;
笛卡尔乘积
*联结条件被省略
*联结条件无效
*第一个表中的所有行与第二个表中的所有行相联结
为避免出现笛卡尔乘积,需要在where中建立有效联结
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1 = table2.column2;
使用联结
可以同时查询多个表
使用表名可以简化前缀
1 最多30字符
2 若使用了该别名,整个select中都要使用该别名
3 别名应具有特殊意义
4 该别名仅对当前select语句有效
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e , departments d
WHERE e.department_id = d.department_id;
非等值联结 利用where between条件联结
SELECT e.last_name, e.salary, j.grade_level
FROM employees e, job_grades j
WHERE e.salary
BETWEEN j.lowest_sal AND j.highest_sal;
自然联结
SELECT department_id, department_name,
location_id, city
FROM departments
NATURAL JOIN locations ;
Ntural join
信息相匹配自动连接
SELECT department_id, department_name,
location_id, city
FROM departments
NATURAL JOIN locations ;
using
连接后的where语句不能再使用别名或表明
SELECT l.city, d.department_name
FROM locations l JOIN departments d
USING (location_id)
WHERE location_id = 1400;
使用On检索数据
SELECT e.employee_id, e.last_name,e.department_id, d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id);
on还可以用于不同对象的联结
SELECT e.last_name emp, m.last_name mgr
FROM employees e JOIN employees m
ON (e.manager_id = m.employee_id);
on用于三向联结
SELECT employee_id, city, department_name
FROM employees e
JOIN departments d
ON d.department_id = e.department_id
JOIN locations l
ON d.location_id = l.location_id;
LEFT OUTER JOIN:
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
LEFT OUTER JOIN departments d
ON (e.department_id = d.department_id) ;
RIGHT OUTER JOIN:
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
RIGHT OUTER JOI departments d
ON (e.department_id = d.department_id) ;
FULL OUTER JOIN:
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON (e.department_id = d.department_id) ;