按照日期查詢(xún)通常有好幾種方法:
按照日期范圍查詢(xún)有好幾種方法,日期字段類(lèi)型一般為:
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'::timest
方法四:
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 遇到主鍵重復(fù)的,怎么能夠跳過(guò)繼續(xù)執(zhí)行呢,其實(shí)很簡(jiǎn)單,就一條一條的插入就可以了,因?yàn)閠o_sql還沒(méi)有很好的解決辦法。
具體的代碼如下所示:
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 無(wú)法設(shè)置主鍵,這個(gè)是肯定的,能做的辦法就是在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");
補(bǔ)充:postgresql 數(shù)據(jù)庫(kù)時(shí)間間隔數(shù)據(jù)查詢(xún)
當(dāng)前時(shí)間向前推一天:
SELECT current_timestamp - interval '1 day'
當(dāng)前時(shí)間向前推一個(gè)月:
SELECT current_timestamp - interval '1 month'
當(dāng)前時(shí)間向前推一年:
SELECT current_timestamp - interval '1 year'
當(dāng)前時(shí)間向前推一小時(shí):
SELECT current_timestamp - interval '1 hour'
當(dāng)前時(shí)間向前推一分鐘:
SELECT current_timestamp - interval '1 min'
當(dāng)前時(shí)間向前推60秒:
SELECT current_timestamp - interval '60 second'
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- PostgreSQL 查看表的主外鍵等約束關(guān)系詳解
- PostgreSQL中enable、disable和validate外鍵約束的實(shí)例
- postgresql 實(shí)現(xiàn)字符串分割字段轉(zhuǎn)列表查詢(xún)
- postgresql 查詢(xún)集合結(jié)果用逗號(hào)分隔返回字符串處理的操作
- postgresql數(shù)據(jù)庫(kù)連接數(shù)和狀態(tài)查詢(xún)操作
- postgresql查詢(xún)自動(dòng)將大寫(xiě)的名稱(chēng)轉(zhuǎn)換為小寫(xiě)的案例
- Postgresql 查詢(xún)表引用或被引用的外鍵操作