简体   繁体   English

如何将SQL结果存储为变量,并在PostgreSQL的后续SQL命令中使用它

[英]How to store a result of SQL as an variable and use it in the later SQL commands in PostgreSQL

I'm trying to catch the first day of current month: 我正在尝试赶上本月的第一天:

select date_trunc('month',  (CURRENT_DATE - INTERVAL '1 day')::date)

and intend to use it in a subsequent SQL command as an variable start_date, like: 并打算在后续的SQL命令中将其用作变量start_date,例如:

select * from table where create_time > start_date;

How to implement that in PostgreSQL? 如何在PostgreSQL中实现呢? Thanks! 谢谢!

with start_date as (
    select date_trunc('month', current_date) as start_date
)
select *
from
    t
    cross join
    start_date
where create_time >= start_date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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