原文传送门:http://blog.csdn.net/wh62592855/archive/2009/11/06/4776631.aspx
记得以前在论坛里看到inthirties用到过WITH AS这个字眼,当时没特别在意。今天在一个帖子里又看到有人用这个,所以就去网上搜了搜相关内容,自己小试了一把,写下来,方便以后忘了的话学习。
===================================================================================
先举个例子吧:
有两张表,分别为A、B,求得一个字段的值先在表A中寻找,如果A表中存在数据,则输出A表的值;如果A表中不存在,则在B表中寻找,若B表中有相应记录,则输出B表的值;如果B表中也不存在,则输出"no records”字符串。
withsql1 as (select to_char(a) s_name from test_tempa),sql2 as (select to_char(b) s_name from test_tempb where not exists (select s_name from sql1 where rownum=1))select * from sql1union allselect * from sql2union allselect 'no records' from dual where not exists (select s_name from sql1 where rownum=1) and not exists (select s_name from sql2 where rownum=1);
再举个简单的例子
with a as (select * from test)
select