sql to sqlalchemy 转换的小例子
代码如下:
select to_char(date_published, 'yyyymm') yo from 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())
相关推荐
liuweiq 2020-06-14
emmm00 2020-06-14
lt云飞扬gt 2020-05-29
朱建伟 2020-05-14
猛禽的编程艺术 2020-05-06
苦咖啡flask 2020-05-04
dreamhua 2020-04-30
dreamhua 2020-04-30
tanrong 2020-04-30
xuanlvhaoshao 2020-04-26
tanrong 2020-04-18
Danialzhou 2020-04-11
happinessaflower 2020-03-01
xiaoxiangyu 2020-02-23
喝绿茶的猫 2020-01-18
疯狂老司机 2020-01-10
liuyang000 2020-01-08
Lingforme 2020-01-08