sql to sqlalchemy 转换
添加时间:2013-5-3 点击量:
to_char(date_published, yyyymm) yo blog group by yo;
转换成sqlalchemy 语句
可以应用 extract
()
In[3]: year_field = db.func.extract(year, ArticleModel.date_published)
In[4]: month_field = db.func.extract(month, ArticleModel.date_published)
In[5]: yonth_field = year_field 100+ month_field
In[6]:print db.session.query(yonth_field.label(yonth), db.func.count(1)).group_by(yonth)
SELECT EXTRACT(year FROM article_meta.date_published):param_1 + EXTRACT(month FROM article_meta.date_published)AS yonth, count(:param_2)AS count_1
FROM article_meta JOIN article ON article_meta.id = article.id GROUPBY yonth
也可以 可以经由过程 func 直接调用数据库支撑的函数或存储过程
yonth = db.func.to_char(ArticleModel.date_published,yyyymm).label(yonth)
archive_list =(db.session.query(yonth,
db.func.count(1)).group_by(yonth).all())
感激python 邮件列表中 jjx 供给帮助
所有随风而逝的都属于昨天的,所有历经风雨留下来的才是面向未来的。—— 玛格丽特·米切尔 《飘》
to_char(date_published, yyyymm) yo blog group by yo;
转换成sqlalchemy 语句
可以应用
extract
()In[3]: year_field = db.func.extract(year, ArticleModel.date_published)
In[4]: month_field = db.func.extract(month, ArticleModel.date_published)
In[5]: yonth_field = year_field 100+ month_field
In[6]:print db.session.query(yonth_field.label(yonth), db.func.count(1)).group_by(yonth)
SELECT EXTRACT(year FROM article_meta.date_published):param_1 + EXTRACT(month FROM article_meta.date_published)AS yonth, count(:param_2)AS count_1
FROM article_meta JOIN article ON article_meta.id = article.id GROUPBY yonth
也可以 可以经由过程 func 直接调用数据库支撑的函数或存储过程
yonth = db.func.to_char(ArticleModel.date_published,yyyymm).label(yonth)
archive_list =(db.session.query(yonth,
db.func.count(1)).group_by(yonth).all())
感激python 邮件列表中 jjx 供给帮助