sql to sqlalchemy 轉換的小例子。本站提示廣大學習愛好者:(sql to sqlalchemy 轉換的小例子)文章只能為提供參考,不一定能成為您想要的結果。以下是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())