如何从sqlalchemy的Date字段中获取月份和年份? [英] How to get month and year from Date field in sqlalchemy?

查看:22
本文介绍了如何从sqlalchemy的Date字段中获取月份和年份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果应该是Date对象

The result should be Date object

由于无法删除"这一天,请将其设置为每月的第 1 天.

Since the day cannot be 'removed', set it to say, 1st day of the month.

只留下月、年

推荐答案

您可以使用以下结构来使用 yearmonth<过滤Date 列/代码>:

You can use following constructs to filter the Date column using either year or month:

.filter(extract('year', Foo.Date) == 2012)
.filter(extract('month', Foo.Date) == 12)

而且 group_by 也是可能的:

.group_by(sqlalchemy.func.year(Foo.Date), sqlalchemy.func.month(Foo.Date))

现在我还没有测试它,但我认为这可能会很慢,因为这些查询会导致全表扫描,因此我建议您花一些时间学习如何使用 复合列.

Now I haven't tested it, but I assume that this might be slow because these queries result in full table scans, therefore I suggest you invest some time and learn how to use composite columns.

这篇关于如何从sqlalchemy的Date字段中获取月份和年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆