Django Query Database Common statements __ Database

Source: Internet
Author: User

Reference: Click to open link

1. Group BY & Order BY & Q

Aggregation + ordering, the corresponding SQL statement is:

Select Currentpid, COUNT (*) from stats_x

where type=3 and (status =1 or Status=0)

GROUP BY Currentpid

ORDER BY currentpid Desc

The corresponding Django notation:

Stats_x.objects.filter (q (type=3) & (Q (status=1) | Q (status=0)). VALUES (' Currentpid '). Annotate (Delta_count=count (' id ')). Order_by ('-currentpid ')

#order by-number is inverted.

#在annotate前面添加values, which is the column with the list of attributes as group by

#annotate, like aggregate, can be used for aggregation, but the column name after aggregate aggregation is fixed and cannot be customized, annotate can customize column names, such as Delta_count

Note: Q expression can not be used between and, must be used &


2. To be Continued ...

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.