Django MySQL不同查询获取多个值 [英] Django MySQL distinct query for getting multiple values

查看:704
本文介绍了Django MySQL不同查询获取多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MySQL 数据库不幸与 Django 1.4.1一起使用。
如果我正确的话,区分功能只适用于POSTGRESQL。



我必须使一个不同的查询由多个值组成,而只有不同的一个,




这一个适用于POSTGRE,但不适用于MYSQL,
我收到以下错误;


DISTINCT ON字段是不支持此数据库后端




  staff = Staff.objects.order_by('person__full_name') .distinct('person__full_name')

然后我尝试了



$ $ $ $ $ $ $ $ $ $$$$$$$$$ >

但是我没有得到不同的价值观,因为我也得到了工作人员的工作类别。但是当我不包含它,我不能得到它作为数组。



任何想法?

解决方案

.distinct([* fields])只适用于PostgresSQL 。



From distinct 文档



这里有区别。对于正常的distinct()调用,数据库会在确定哪些行不同时比较每行中的每个字段。对于具有指定字段名称的distinct()调用,数据库将仅比较指定的字段名称。



如所述,记录中的所有字段都被检查。很可能在你的情况下,你得到不同字段值的记录(更可能的情况是,如果你查询多个表ManyToMany或ForeignKey关系)。



为了合并为数组可以参考以前的问题 Django查询不同价值观工作,但我无法使用查询结果


I have a MySQL database unfortunately used with Django 1.4.1. Distinct function is only working for POSTGRESQL if i get it right.

I have to make a distinct query consist of multiple values while only distinct one,

Like; This one works for POSTGRE but not with MYSQL, I get the following error;

DISTINCT ON fields is not supported by this database backend

staff = Staff.objects.order_by('person__full_name').distinct('person__full_name')

Then I tried

staff = Staff.objects.values('person__full_name','staff_job_categories').distinct().order_by('person__full_name')

But I do not get distinct values because i get also the staff job categories. But when I dont contain it I could not get it as array.

Any idea ?

解决方案

.distinct([*fields]) only works in PostgresSQL.

From distinct documentation

Here's the difference. For a normal distinct() call, the database compares each field in each row when determining which rows are distinct. For a distinct() call with specified field names, the database will only compare the specified field names.

As stated all fields in a record are checked. Mostly likely in your case you are getting records with different field values (more likely a case if you are queries multiple tables ManyToMany or ForeignKey relations).

For consolidating as array you can refer your earlier question Django Query distinct values works but i cant use the query result

这篇关于Django MySQL不同查询获取多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆