首页 > 解决方案 > 获取时间戳不是今天日期的记录

问题描述

我有如下代码来获取今天的日期:

today = str(date.today()) //2019-01-07

我想从数据库中获取timestamp列与今天不同的所有记录。功能如下:

def get_vehicle_references():
  conn = db_connection()
  cur = conn.cursor()
  s = "SELECT reference, reference_url FROM vehicles v WHERE (NOT EXISTS (select reference from daily_run_vehicle rv WHERE (handled = %s or (handled = %s and retries >= %s)) and DATE(rv.timestamp) <> %s AND reference = v.reference))"
  cur.execute(s, (True, False, 5, today))
  return cur.fetchall()

时间戳列采用以下格式:2019-01-07 11:10:18.491368

我想从vehicles表中timestamp的列daily_run_vehicle不是今天的表中获取所有记录。

但是这个查询也给了我今天日期的记录。

任何想法?

更新:

因此我尝试对其进行转换,查询如下:

s = "SELECT reference, reference_url, timestamp FROM vehicles v WHERE (NOT EXISTS (select reference from daily_run_vehicle rv WHERE (handled = %s or (handled = %s and retries >= %s)) AND rv.timestamp::date = %s AND reference = v.reference))"
cur.execute(s, (True, False, 5, today))

但我仍然有同样的问题。

标签: pythonpostgresql

解决方案


推荐阅读