将选定的日期时间转换为 sqlalchemy 中的日期

编程入门 行业动态 更新时间:2024-10-07 00:13:10
本文介绍了将选定的日期时间转换为 sqlalchemy 中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个测试记录数据库,其中一列test_time"定义为日期时间.我想查询有多少个不同的日期,因为我想根据日期将测试结果转储到 csv.我现在有以下内容:

I have a database of test records with one column 'test_time' defined as datetime. I want to query how many distinct dates there are as I want to dump test results to csv according to dates. I now have the following:

distinct_dates = list(session.query(Test_Table.test_time).distinct())

但这给了我一个日期时间而不是日期的列表.当然我可以在 Python 中转换它,但是当我使用 sqlite 时.我做了这个SELECT DISTINCT DATE(test_time) FROM Test_Table.我无法想出 sqlalchemy 中的等价物.

But this gives me a list of datetime not date. Certainly I can convert it in Python but when I am using sqlite. I did this SELECT DISTINCT DATE(test_time) FROM Test_Table. I cannot come up with the equivalent in sqlalchemy.

推荐答案

那将是使用 cast() 表达式:

That would be by using the cast() expression:

from sqlalchemy import cast, Date distinct_dates = session.query(cast(Test_Table.test_time, Date)).distinct().all()

或快捷方法:

distinct_dates = session.query(Test_Table.test_time.cast(Date)).distinct().all()

如果使用 SQLite,试一试:

and if using SQLite, give this a shot:

distinct_dates = session.query(func.DATE(Test_Table.test_time)).distinct().all()

更多推荐

将选定的日期时间转换为 sqlalchemy 中的日期

本文发布于:2023-10-23 02:35:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   转换为   时间   sqlalchemy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!