postgresql数据库使用说明_实现时间范围查询
按照日期查询通常有好几种方法:
按照日期范围查询有好几种方法,日期字段类型一般为:
Timestampwithouttimezone
方法一:
select*fromuser_infowherecreate_date >='2015-07-01'andcreate_date<'2015-08-15';
方法二:
select*fromuser_infowherecreate_date between'2015-07-01'and'2015-08-15';
方法三:
select*fromuser_infowherecreate_date >='2015-07-01'::timestampandcreate_date<'2015-08-15'::timestamp;
方法四:
select*fromuser_infowherecreate_date
betweento_date('2015-07-01','YYYY-MM-DD')andto_date('2015-08-15','YYYY-MM-DD');
pandas.to_sql遇到主键重复的,怎么能够跳过继续执行呢,其实很简单,就一条一条的插入就可以了,因为to_sql还没有很好的解决办法。
具体的代码如下所示:
forexchangeinexchange_list.items():
ifexchange[1]==True:
pass
else:
continue
sql="""SELECT*FROM%sWHERE"time"BETWEEN'2019-07-0518:48'AND'2019-07-09'"""%(exchange[0])
data=pd.read_sql(sql=sql,con=conn)
print(data.head())
foriinrange(len(data)):
#sql="SELECT*FROM`%s`WHERE`key`='{}'"%(exchange).format(row.Key)
#found=pd.read_sql(sql,con=conn2)
#iflen(found)==0:
try:
data.iloc[i:i+1].to_sql(name=exchange[0],index=False,if_exists='append',con=conn2)
exceptExceptionase:
print(e)
pass
pandas.to_sql无法设置主键,这个是肯定的,能做的办法就是在to_sql之前先使用创建表的方法,创建一张表
建表的代码如下所示:
/*
CreateSEQUENCEfortable
*/
DROPSEQUENCEIFEXISTS@exchangeName_id_seq;
CREATESEQUENCE@exchangeName_id_seq
STARTWITH1
INCREMENTBY1
NOMINVALUE
NOMAXVALUE
CACHE1;
/*
CreateTablestructurefortable
*/
DROPTABLEIFEXISTS"public"."@exchangeName";
CREATETABLE"public"."@exchangeName"(
"id"int4NOTNULLDEFAULTnextval('@exchangeName_id_seq'::regclass),
"time"timestamp(6)NOTNULL,
"open"float8,
"high"float8,
"low"float8,
"close"float8,
"volume"float8,
"info"varcharCOLLATE"pg_catalog"."default"NOTNULL
)
;
/*
CreatePrimaryKeystructurefortable
*/
ALTERTABLE"public"."@exchangeName"DROPCONSTRAINTIFEXISTS"@exchangeName_pkey";
ALTERTABLE"public"."@exchangeName"ADDCONSTRAINT"@exchangeName_pkey"PRIMARYKEY("time","info");
补充:postgresql数据库时间间隔数据查询
当前时间向前推一天:
SELECTcurrent_timestamp-interval'1day'
当前时间向前推一个月:
SELECTcurrent_timestamp-interval'1month'
当前时间向前推一年:
SELECTcurrent_timestamp-interval'1year'
当前时间向前推一小时:
SELECTcurrent_timestamp-interval'1hour'
当前时间向前推一分钟:
SELECTcurrent_timestamp-interval'1min'
当前时间向前推60秒:
SELECTcurrent_timestamp-interval'60second'
以上为个人经验,希望能给大家一个参考,也希望大家多多支持毛票票。如有错误或未考虑完全的地方,望不吝赐教。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。