[Django/mysql] How to query multiple non-repeated record values in mysql using distinct, djangodistinct

Source: Internet
Author: User

[Django/mysql] How to query multiple non-repeated record values in mysql using distinct, djangodistinct

No nonsense. directly go to the text

Body:

How can I use distinct to query multiple non-repeated record values in mysql?

First, we must know that there are two ways to execute a query in the django model:

First, use the api provided by django, such as the filter value distinct order_by model query api;

Code: LOrder. objects. values ('finish _ Time'). distinct ()

Note the following:

Example (the first and later examples can only work on PostgreSQL ):

>>> Author.objects.distinct()[...]>>> Entry.objects.order_by('pub_date').distinct('pub_date')[...]>>> Entry.objects.order_by('blog').distinct('blog')[...]>>> Entry.objects.order_by('author', 'pub_date').distinct('author', 'pub_date')[...]>>> Entry.objects.order_by('blog__name', 'mod_date').distinct('blog__name', 'mod_date')[...]>>> Entry.objects.order_by('author', 'pub_date').distinct('author')

Because of the mysql database I use, distinct can only be the first usage, or

LOrder. objects. values ('finish _ Time'). distinct (). order_by ('finish _ Time ')

Second, use the original SQL query

LOrder. objects. raw ('select DISTINCT id, finish_time FROM keywork_lorder group by finish_time ')

Use the mysql statement to remove duplicates.Special noteIs:

First, there is only one field in the original SQL query that cannot be discarded, as stated in the official document:

Only one field cannot be omitted-it is the primary key. Django uses the primary key to identify the model instance, so it must be included in each original query. If you forget to include the primary key,InvalidQueryException.

If your SQL statement is like 'select DISTINCT finish_time FROM keywork_lorder ', the Raw query must include the primary key, that is, the id field cannot be discarded!

Second, here is the original mysql query statement. To remove duplicate items in mysql, write 'select DISTINCT id, finish_time FROM keywork_lorder group by finish_time 'as follows'

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.