Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of sql to sqlalchemy Transformation

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail the case analysis of sql to sqlalchemy conversion for you. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The code is as follows:

Select to_char (date_published, 'yyyymm') yo from blog group by yo

You can use extract () to convert to a sqlalchemy statement

The code is as follows:

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

You can also call functions or stored procedures supported by the database directly through func

The code is as follows:

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 ()

This is the end of the case analysis of sql to sqlalchemy conversion. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report