简体   繁体   中英

Oracle Alias Query Results

I want to alias the results of one query, and use said alias in another. As a simple example, something like:

(select a from foo) as fooResults;

select b from bar
  where b in fooResults;

This syntax doesn't work, clearly. Is there a way to accomplish this with vanilla Oracle SQL? If it requires PL/SQL, what is the syntax? Search results all seem to point to column or table aliases.

EDIT: One driving factor is that I want to be able to use fooResults in several queries later, if possible.

There are a few options. The cleanest, assuming you want to retain the alias, is probably to use the WITH clause

WITH fooResults
  AS( SELECT a
        FROM foo )
SELECT b
  FROM bar
 WHERE b IN (SELECT a
               FROM fooResults)

If you want to use the expression in multiple queries, you would realistically want to define fooResults as a view.

Whether fooResults is a view, an inline view, or a subquery defined in a WITH clause, though, you'll have to SELECT from it in your IN clause. You can't do WHERE b IN fooResults no matter how fooResults is defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM