从SQLAlchemy中的时间戳中选择日期 [英] Select date from timestamp in SQLAlchemy

查看:259
本文介绍了从SQLAlchemy中的时间戳中选择日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将SQLAlchemy与python结合使用,我想从列类型时间戳中选择日期,以执行以下查询:

I'm using SQLAlchemy with python and i want to select date from column type timestamp, to do this query:

SELECT DATE(`record_date`) FROM Users

我通过sql alchemy编写了此代码,但是它将返回时间戳:

I made this code by sql alchemy but it will return the timestamp:

session.query(Users.record_date).all()

我该怎么办?

推荐答案

提供别名

  SELECT DATE(`record_date`) as record_d FROM Users

然后使用此

 session.query(Users.record_d).all()

如果要直接这样做,请尝试

If want do it directly then ,try this

your_dates = session.query(cast(Users.record_date, DATE)).all()

your_dates = session.query(func.DATE(Users.record_date)).all()

这篇关于从SQLAlchemy中的时间戳中选择日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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