资源描述:
《oracle中join学习个人笔记》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、oracle中join学习个人笔记2010-09-2623:49:25分类:Linux3joins3.2连接条件using相同的列连接可用using子句,且select列中不能对条件列限定表名using子句可使用多个列,如using(a,b)SELECTlocation_id,department.name,location.regional_groupFROMdepartmentJOINlocationUSING(location_id);naturaljoin无需使用using子句selectlocation_id
2、,department.name,location.regional_groupfromlocationnaturaljoindepartment会自动匹配两个表中所有相同的列,影响输出3.3连接类型crossjoin笛卡尔积不能带条件,如on子句innerjoin内连接默认的连接,inner可省略outerjoin外连接FROMtable1{LEFT
3、RIGHT
4、FULL}[OUTER]JOINtable2left:返回table1中的所有记录,table2对应列显示空right:返回table2中的所有记录,tab
5、le1对应列显示空full:不匹配的记录全部显示out:默认可缺省,oracle会自动在left,right,full后增加outleftoutjoinselectd.dept_id,d.name,l.regional_groupfromdepartmentdleftouterjoinlocationlond.location_id=l.location_id早期的写法selectd.dept_id,d.name,l.regional_groupfromdepartmentd,locationlwhered.locat
6、ion_id=l.location_id(+)返回department表里所有记录rightoutjoinselectd.dept_id,d.name,l.regional_groupfromdepartmentdrightouterjoinlocationlond.location_id=l.location_id早期的写法selectd.dept_id,d.name,l.regional_groupfromdepartmentd,locationlwhered.location_id(+)=l.location_i
7、d返回location表里所有记录fulloutjoin两张表不匹配的记录都显示相等连接与不等连接不等连接示例selectp.namepart_name,c.inv_classinv_classfrompartpjoininventory_classconp.unit_costbetweenc.low_costandc.high_costselfjoin自联接Selfouterjoinsselecte.lnameemployee,m.lnamemanagerfromemployeeeleftouterjoinemplo
8、yeemone.manager_emp_id=m.emp_idpartitionouterjoinsoracle10g的新特性,与lag,lap查询有关系selectnvl(ee.emp_id,7782),m.year,m.month,nvl(ee.expense_claim,0)from(select*frommonthswhereyear=2002)mleftouterjoin(select*fromemployee_expensewhereemp_id=7782)eeonm.month=ee.monthandm.
9、year=ee.yearorderbym.month可用partitionouterjoins实现SELECTee.emp_id,m.year,m.month,NVL(ee.expense_claim,0)FROM(SELECT*FROMmonthsWHEREyear=2002)mLEFTOUTERJOINemployee_expenseeePARTITIONBY(ee.emp_id)ONm.year=ee.yearANDm.month=ee.monthORDERBYee.emp_id,m.month;原理首先对emp
10、loyee_expense按emp_id分组,一个值一组然后每组都与months表联接,会有多个外联接操作3.4joinsandsubqueries联接与子查询SELECTsupplier_id,nameFROMsuppliersWHEREEXISTS(SELECT*FROMpartpWHEREp.inventory_qty<10