postgresql數(shù)據(jù)庫使用說明_實現(xiàn)時間范圍查詢
按照日期查詢通常有好幾種方法:
按照日期范圍查詢有好幾種方法,日期字段類型一般為:
Timestamp without timezone
方法一:
select * from user_info where create_date >= '2015-07-01' and create_date < '2015-08-15';
方法二:
select * from user_info where create_date between '2015-07-01' and '2015-08-15';
方法三:
select * from user_info where create_date >= '2015-07-01'::timestamp and create_date < '2015-08-15'::timestamp;
方法四:
select * from user_info where create_date between to_date('2015-07-01','YYYY-MM-DD') and to_date('2015-08-15','YYYY-MM-DD');
pandas.to_sql 遇到主鍵重復的,怎么能夠跳過繼續(xù)執(zhí)行呢,其實很簡單,就一條一條的插入就可以了,因為to_sql還沒有很好的解決辦法。
具體的代碼如下所示:
for exchange in exchange_list.items(): if exchange[1]==True: pass else: continue sql = """ SELECT * FROM %s WHERE "time" BETWEEN '2019-07-05 18:48' AND '2019-07-09' """ % (exchange[0]) data = pd.read_sql(sql=sql, con=conn) print(data.head()) for i in range(len(data)): #sql = "SELECT * FROM `%s` WHERE `key` = '{}'"%(exchange).format(row.Key) #found = pd.read_sql(sql, con=conn2) #if len(found) == 0: try: data.iloc[i:i + 1].to_sql(name=exchange[0], index=False,if_exists='append', con=conn2) except Exception as e: print(e) pass
pandas.to_sql 無法設置主鍵,這個是肯定的,能做的辦法就是在to_sql之前先使用創(chuàng)建表的方法,創(chuàng)建一張表
建表的代碼如下所示:
/* Create SEQUENCE for table */ DROP SEQUENCE IF EXISTS @exchangeName_id_seq; CREATE SEQUENCE @exchangeName_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1; /* Create Table structure for table */ DROP TABLE IF EXISTS "public"."@exchangeName"; CREATE TABLE "public"."@exchangeName" ( "id" int4 NOT NULL DEFAULT nextval('@exchangeName_id_seq'::regclass), "time" timestamp(6) NOT NULL, "open" float8, "high" float8, "low" float8, "close" float8, "volume" float8, "info" varchar COLLATE "pg_catalog"."default" NOT NULL ) ; /* Create Primary Key structure for table */ ALTER TABLE "public"."@exchangeName" DROP CONSTRAINT IF EXISTS "@exchangeName_pkey"; ALTER TABLE "public"."@exchangeName" ADD CONSTRAINT "@exchangeName_pkey" PRIMARY KEY ("time", "info");
補充:postgresql 數(shù)據(jù)庫時間間隔數(shù)據(jù)查詢
當前時間向前推一天:
SELECT current_timestamp - interval '1 day'
當前時間向前推一個月:
SELECT current_timestamp - interval '1 month'
當前時間向前推一年:
SELECT current_timestamp - interval '1 year'
當前時間向前推一小時:
SELECT current_timestamp - interval '1 hour'
當前時間向前推一分鐘:
SELECT current_timestamp - interval '1 min'
當前時間向前推60秒:
SELECT current_timestamp - interval '60 second'
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
解決postgresql 數(shù)據(jù)庫 update更新慢的原因
這篇文章主要介紹了解決postgresql 數(shù)據(jù)庫 update更新慢的原因,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01postgresql 切換 log、xlog日志的實現(xiàn)
這篇文章主要介紹了postgresql 切換 log、xlog日志的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01PostgreSQL進行數(shù)據(jù)導入和導出的操作代碼
在數(shù)據(jù)庫管理中,數(shù)據(jù)的導入和導出是非常常見的操作,特別是在 PostgreSQL 中,提供了多種工具和方法來實現(xiàn)數(shù)據(jù)的有效管理,本文將詳細介紹在 PostgreSQL 中如何進行數(shù)據(jù)導入和導出,并給出具體的命令及示例,需要的朋友可以參考下2024-10-10PostgreSQL的generate_series()函數(shù)的用法說明
這篇文章主要介紹了PostgreSQL的generate_series()函數(shù)的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01PostgreSQL使用jsonb進行數(shù)組增刪改查的操作詳解
有時候我們需要使用PostgreSQL這種結構化數(shù)據(jù)庫來存儲一些非結構化數(shù)據(jù),PostgreSQL恰好又提供了json這種數(shù)據(jù)類型,這里我們來簡單介紹使用jsonb的一些常見操作,需要的朋友可以參考下2024-03-03PostgreSQL pg_ctl start啟動超時實例分析
這篇文章主要給大家介紹了關于PostgreSQL pg_ctl start啟動超時的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01