1. 程式人生 > >PGSQL查詢今天,昨天的資料,一個月之內的資料

PGSQL查詢今天,昨天的資料,一個月之內的資料

PGSQL查詢今天的資料

 select	*
	from 表名 as n
	where  n.create_date>=current_date;

PG查詢昨天的資料:

方法1:

 select	*
	from 表名 as n
	where
             age(
                current_date,to_timestamp(substring(to_char(n.create_date, 'yyyy-MM-dd hh24 : MI : ss' ) FROM 1 FOR 10),'yyyy-MM-dd')) ='1 days';

方法2:

 select	*
	from 表名 as n
	where  n.create_date>=current_date-1;

n.create_date 是一個timestamp的資料;

current_date是pgsql資料一個獲取當前日期的欄位;

to_char(timestamp,text)把timestamp資料轉換成字串;

substring(text from int for int) 擷取想要的文字格式 ‘yyyy-MM-dd’;

to_timestamp(text,'yyyy-MM-dd')轉換成timestamp格式;

age(timestamp,timestamp)獲取兩個時間之差 返回 days

PG查詢最近一個月內的資料

select *
	from 表名 as n
	and n.create_date>=to_timestamp(substring(to_char(now(),'yyyy-MM-dd hh24:MI:ss') FROM 1 FOR 10),'yyyy-MM-dd')- interval '30 day';