A small example of SQL to sqlalchemy Conversion

Source: Internet
Author: User

Copy codeThe Code is as follows:
Select to_char (date_published, 'yyymmm ') yo from blog group by yo;

Convert to sqlalchemy statement


You can use extract ()
Copy codeThe 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 use func to directly call functions or stored procedures supported by the database.
Copy codeThe Code is as follows:
Yonth = db. func. to_char (ArticleModel. date_published, 'yyyml'). label ('yonth ')
Archive_list = (db. session. query (yonth,
Db. func. count (1). group_by ('yonth'). all ())

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.