按照日期查询通常有好几种方法:

按照日期范围查询有好几种方法,日期字段类型一般为:

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 遇到主键重复的,怎么能够跳过继续执行呢,其实很简单,就一条一条的插入就可以了,因为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之前先使用创建表的方法,创建一张表

建表的代码如下所示:

/*
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 数据库时间间隔数据查询

当前时间向前推一天:

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,时间范围查询

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。