简体   繁体   中英

Alias a POSTGRES select statement

I want to create new tables in the query and use them at the end for a final query. I don't remember what the syntax is and can't find it anywhere. (here is a basic example for illustration only).

phones as 
    (
        SELECT phone_number from customers
    )


emails as 
    (
        SELECT emails from customers
    )

// do something with both

You are looking for CTE's

With phones as 
    (
        SELECT phone_number from customers
    )
,emails as 
    (
        SELECT emails from customers
    )
select * from phones--/emails 

You are looking for a Common Table Expression (CTE). These are introduced using with :

with phones as (
      SELECT phone_number from customers
     ),
     emails as (
      SELECT emails from customers
    )
select . . .;

Your example selects don't seem particularly useful, but I assume they are just for illustration.

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