oracle sql 查看文章
oracle sql 学习笔记(一)2008-08-15 17:00----#############选择与投影#############------
1.空元素处理
select salary*12*(1+nvl(commission_pct,0)/100) as salary from s_emp;//nvl(one,two): one==null?two:one
2.select 表达式
select salary*12 "year salary" from s_emp;
select first_name||' '||last_name "name" from s_emp;
3.distinct的使用
select distinct dept_id,title from s_emp;//联合唯一
select dept_id,distinct title from s_emp;//出错
4.where的使用
select salary from s_emp where first_name='Carmen';//加双引号有误,被系统当作别名解释
故字符应使用''修饰,并且大小写敏感(where运行在select之前,即先筛选再投影)
5.between and (包含两端)
select first_name,salary from s_emp where salary between 1500 and 2000;
6.in any
select first_name from s_emp where dept_id in(41,42,32)
select first_name from s_emp where dept_id = any(41,42,32)//两语句等价
select first_name from s_emp where dept_id<>all(41,42,32)
7.like
select table_name from user_tables where table_name like 'S/_%' escape '/' ;//选择以S_开头的表名
select table_name from user_tables where table_name like 'S_%';选择以S?*开始的表明
SELECT [DISTINCT]{*,column[alias],...}
FROM table
[WHERE condition(s)]
[ORDER BY {column,expr,alias}[ASC|DESC]]
------############Oracle函数###########-------
1.字符函数
LOWER('String') =-> string
Upper('String') =-> STRING
Initcap('string')=-> String//首字母大写
select lower(first_name) from s_emp;
select lower('Sql Cource') from s_emp;
select salary,first_name from s_emp where lower(first_name)='carmen';//查询一个叫carmen的工资,字符大小写未知
CONCAT('GOOD','String')=->GOODString
SUBSTR('String',1,3)=->Str
LENGTH('String')=->6
select substr(first_name,length(first_name)-1,2) from s_emp;
select substr(first_name,-2,2) from s_emp;//-2表示从右往左数两位,故上述两句意义相同
2.数学函数
ROUND(45.923,2)=->45.92
ROUND(45.923,0)=->46
ROUND(45.923,-1)=->50//四舍五入
TRUNC(45.923,2)=->45.92
TRUNC(45.923)=->45
TRUNC(45.923,-1)=->40//截取
3.日期类型//所有使用缺省日期隐式转换的,必须保证日期格式与缺省格式一致,否则为防止报错应显式转换
select sysdate from dual;//显示系统日期
alter session set nls_data_format ='yyyy mm dd hh24:mi:ss'//改变时间格式
select sysdate+1/144,sysdate from dual;//十分钟之后
MONTH_BETWEEN('01-SEP-95','11-JAN-94')=->19.774194
ADD_MONTHS('11-JAN-94',6)=->'11-JUL-94'
NEXT_DAY('01-SEP-95','FRIDAY')=-'08-SEP-95'
LAST_DAY('01-SEP-95')=->'30-SEP-95'
ROUND(SYSDATE,'MONTH')=->01-JUN-95
TRUNC(SYSDATE,'MONTH')=->01-MAY-95
TO_DATE('01-SEP-95','DD-MM-YY')
TO_CHAR(SYSDATE,'DD-MM-YY')//
SELECT TRUNC(ADD_MONTHS(SYSDATE,1),'MONTH') FROM DUAL;//选出下个月的一号
SELECT last_day(sysdate)+1 FROM DUAL;//同上
//SELECT TRUNC(ADD_MONTHS('24-JUN-95',1),'MONTH') FROM DUAL;//出错
select first_name,to_char(start_date,'yyyy mm dd hh24:mi:ss') from s_emp;//显式数据类型转换实现按指定格式显示
select first_name,start_date from s_emp where dept_id=42 等价于select first_name,tochar(start_date,'yyyy mm dd')
4.数字转换//to_char and to_number
SELECT TO_NUMBER('AB','XX') FROM DUAL;//16进制转为十进制
select to_char(next_day(add_months(date_ordered,6),'Friday'),'fmday,month ddth,YYYY') "NEW 6 MONTH REVIEW"
FROM S_ORD
ORDER BY DATE_ORDERED;
----############多表查询##############--------------
#先考虑多表是哪些
#再考虑wheretiaojian
#最后考虑投影
#-------------------
1,笛卡尔积
select first_name,dept_id,s_dept.id,name from s_emp ,s_dept;
2.等值连接//又称为内连接,精确匹配(inner join)
select first_name,dept_id,s_dept.id,name from s_emp ,s_dept where s_emp.dept_id=s_dept.id;
select s_dept.name,s_region.name from s_dept,s_region where s_dept.REGION_ID=s_region.ID; //选部门名对应的地区名
select first_name,s_region.name from s_emp,s_dept,s_region where first_name='Carmen' and s_emp.dept_id =s_dept.id
and s_dept.region_id=s_region.id; //查出Carmen在哪个地区上班
select e.first_name,m.first_name from s_emp e,s_emp m where e.manager_id=m.id;// 找出员工及其对应的领导(自联结)
select distinct m.first_name from s_emp e,s_emp m where e.manager_id=m.id; //哪些人是领导
3.外联结(左外连接 left Join ,右外联结right join)
select e.first_name,m.first_name from s_emp e,s_emp m where e.manager_id=m.id(+);//防止e表中条目丢失
select e.first_name,m.first_name from s_emp e left join s_emp m on e.manager_id=m.id;//同上(左连接)