當(dāng)前位置:首頁 >  站長 >  數(shù)據(jù)庫 >  正文

postgresql數(shù)據(jù)庫使用說明 實(shí)現(xiàn)時(shí)間范圍查詢

 2021-05-21 16:58  來源: 腳本之家   我來投稿 撤稿糾錯(cuò)

  域名預(yù)訂/競價(jià),好“米”不錯(cuò)過

這篇文章主要介紹了postgresql數(shù)據(jù)庫使用說明_實(shí)現(xiàn)時(shí)間范圍查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧。

按照日期查詢通常有好幾種方法:

按照日期范圍查詢有好幾種方法,日期字段類型一般為:

1Timestamp 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 遇到主鍵重復(fù)的,怎么能夠跳過繼續(xù)執(zhí)行呢,其實(shí)很簡單,就一條一條的插入就可以了,因?yàn)閠o_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 無法設(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ù)庫時(shí)間間隔數(shù)據(jù)查詢

當(dāng)前時(shí)間向前推一天:

1SELECT current_timestamp - interval '1 day'

當(dāng)前時(shí)間向前推一個(gè)月:

1SELECT current_timestamp - interval '1 month'

當(dāng)前時(shí)間向前推一年:

1SELECT current_timestamp - interval '1 year'

當(dāng)前時(shí)間向前推一小時(shí):

1SELECT current_timestamp - interval '1 hour'

當(dāng)前時(shí)間向前推一分鐘:

1SELECT current_timestamp - interval '1 min'

當(dāng)前時(shí)間向前推60秒:

1SELECT current_timestamp - interval '60 second'

文章來源:腳本之家

來源地址:https://www.jb51.net/article/204935.htm

申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)文章

熱門排行

信息推薦