简体   繁体   English

按日期过滤 sqlalchemy sqlite 日期时间列

[英]Filter sqlalchemy sqlite datetime column by date

I have a datetime column in a database (Named TestBase) like so:我在数据库(命名为 TestBase)中有一个日期时间列,如下所示:

INDEX   DateTimeColumn
...     ...
1241    2010-07-26 07:04:05 
1242    2010-07-26 07:04:07 
1243    2010-07-26 07:04:12 
1244    2010-07-27 10:59:53

I want to get all the unique days in the column.我想获得列中所有独特的日子。

However, using the distinct function in sqlalchemy as such: distinct(TestBase.DateTimeColumn) does not work.但是,在 sqlalchemy 中使用不同的 function : distinct(TestBase.DateTimeColumn)不起作用。 As it takes the time within the timestamp into account and considers every entry to be unique.因为它需要考虑时间戳内的时间,并认为每个条目都是唯一的。

How do I write a query which ignores the time within a timestamp and just returns the list of unique dates?如何编写一个忽略时间戳内的时间并只返回唯一日期列表的查询?

Use the database's DATE function to truncate the datetime to a date.使用数据库的DATE function 将日期时间截断为日期。 The DATE function is accessed through sqlalchemy.sql.func : DATE function 通过sqlalchemy.sql.func访问:

from sqlalchemy import sql

q = session.query(sql.func.date(MyModel.datetimecolumn))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM