研究时测试用的例子:xtest2是基表,xtest1是外表表结构及数据如下
xtest1(外表) company_code item_code item_name cost_class
00 item1 name1 f
00 item2 name2 m
01 item1 name1 m
01 item2 name2 f考试大-全国最大教育类网站(www.Examda。com)
xtest2(基表) company_code item_code t1 t2
00 item1 aa bb
00 item2 cc dd
00 item3 ee ff
null null null null
要求:将xtest2中的数据全部显出,根据xtest2中的公司号及物料代码去xtest1表里找对应的物料名,但是有一个附加条件,就是xtest1表里必须是cost_class='f'的才能写到最后的结果集中。最后实验成功的sql如下:
select a.item_code,b.item_name from xtest2 a,xtest1 b where a.item_code = b.item_code(+) and b.cost_class(+)='f'
and a.company_code = b.company_code(+)
总结:b表的匹配条件还要加一个右连接,要不无法得到想要的结果。
责任编辑:小草