主机系统-postgresql数据库使用说明 实现时间范围查询

这篇文章首要介绍了主机系统postgresql数据库运用说明_完成时刻规模查询,具有很好的参考价值,期望对我们有所协助。一同跟从小编过来看看吧。

依照日期查询一般有好几种办法:

依照日期规模查询有好几种办法,日期字段类型一般为:

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 遇到主键重复的,怎样可以越过持续履行呢,其实很简单,就一条一条的刺进就可以了,由于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 数据库时刻距离数据查询

当时时刻向前推一天:

1SELECT current_timestamp – interval ‘1 day’

当时时刻向前推一个月:

1SELECT current_timestamp – interval ‘1 month’

当时时刻向前推一年:

1SELECT current_timestamp – interval ‘1 year’

当时时刻向前推一小时:

1SELECT current_timestamp – interval ‘1 hour’

当时时刻向前推一分钟:

1SELECT current_timestamp – interval ‘1 min’

当时时刻向前推60秒:

1SELECT current_timestamp – interval ’60 second’