Django distinct重复数据 Django 解决distinct无法去除重复数据的问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Django distinct重复数据 Django 解决distinct无法去除重复数据的问题

CQ-LQJ   2021-04-22 我要评论

使用distinct在mysql中查询多条不重复记录值的解决办法

如何使用distinct在mysql中查询多条不重复记录值?

有时候想用distinct去掉queryset中的重复项,看django文章中是这么说的

>>> 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')
[...]
Note

django文档中特别介绍了,distinct的列一定要先order_by并且在第一项。
When you specify field names, you must provide an order_by() in the QuerySet, and the fields in order_by() must start with the fields in distinct(), in the same order.

For example, SELECT DISTINCT ON (a) gives you the first row for each value in column a. If you don't specify an order, you'll get some arbitrary row.


完全照做,用的mysql数据库最后出现了这样的警告:

 raise NotImplementedError('DISTINCT ON fields is not supported by this database backend')
NotImplementedError: DISTINCT ON fields is not supported by this database backend
告诉我数据库不支持。

当然可以这样:

items = []
for item in query_set:
  if item not in items:
    items.append(item)

首先,我们必须知道在django中模型执行查询有两种方法:

第一种,使用django给出的api,例如filter value distinct order_by等模型查询api;

代码:LOrder.objects.values('finish_time').distinct()

这里应注意,原官方文档中写到:

示例(第一个之后的示例都只能在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')

因为我使用的mysql数据库,所以在distinct只能是第一中用法,或者可以这样用

LOrder.objects.values('finish_time').distinct().order_by('finish_time')

第二种,使用原始SQL查询

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

上面直接使用mysql语句进行剔重,这里需要特别注意的是:

一是原始SQL查询只有一种字段不可以被丢掉,官方文档中这样说道:

只有一种字段不可以被省略——就是主键。 Django 使用主键来识别模型的实例,所以它在每次原始查询中都必须包含。 如果你忘记包含主键的话,会抛出一个InvalidQuery异常。

意思是,如果你的sql语句是这样的'SELECT DISTINCT finish_time FROM keywork_lorder ',那么将会报错Raw query must include the primary key,就是id字段不能被丢掉!

二是,这里是原始mysql查询语句,mysql去掉重复项要这样写:'SELECT DISTINCT id,finish_time FROM keywork_lorder group by finish_time'

补充:使用Distinct去除重复数据

distinct用于在查询中返回列的唯一不同值(即去重复),支持单列或多列。
在实际的应用中,表中的某一列含有重复值是很常见的,如employee员工表的dept部门列。
如果在查询数据时,希望得到某列的所有不同值,可以使用distinct。

distinct 语法

select 【distinct】 column_name1,column_name2
from table_name;

下面开始操作

创建一个足迹表
create table footprint(
	id int not null auto_increment primary key,
	username varchar(30) comment '用户名',
	city varchar(30) comment '城市',
	visit_date varchar(10) comment '到访日期'
);
插入一些数据
insert into footprint(username, city, visit_date) values('mofei', '贵阳', '2019-12-05');
insert into footprint(username, city, visit_date) values('mofei', '贵阳', '2020-01-15');
insert into footprint(username, city, visit_date) values('mofei', '北京', '2018-10-10');
insert into footprint(username, city, visit_date) values('zhangsan', '上海', '2020-01-01');
insert into footprint(username, city, visit_date) values('zhangsan', '上海', '2020-02-02');
insert into footprint(username, city, visit_date) values('lisi', '拉萨', '2016-12-20');

这些用户到访过那些城市
mysql> select distinct city from footprint;
和group by 效果相同,只不过distinct专门负责去重复这个活
mysql> select city from footprint group by city;


查询有几个用户在使用系统
mysql> select distinct username from footprint;

dictinct作用于两个字段时,多条数据都相同时会保留一条

以上内容来自墨菲墨菲的补充

补充知识:Distinct和Group by去除重复字段记录

重复记录 有两个意义,一是完全重复的记录,也即所有字段均重复的记录

二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。

1、对于第一种重复,比较容易解决,使用

select distinct * from tableName

就可以得到无重复记录的结果集。

如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除

select distinct * into #Tmp from tableName
drop table tableName
select * into tableName from #Tmp
drop table #Tmp

发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。

2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下

假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集

select identity(int,1,1) as autoID, * into #Tmp from tableName
select min(autoID) as autoID into #Tmp2 from #Tmp group by Name
select * from #Tmp where autoID in(select autoID from #tmp2)

最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)

其它的数据库可以使用序列,如:

create sequence seq1;
select seq1.nextval as autoID, * into #Tmp from tableName

zuolo: 我根据上面实例得到所需要的语句为 SELECT MAX(id) AS ID,Prodou_id,FinalDye FROM anwell..tblDBDdata GROUP BY Prodou_id,FinalDye ORDER BY id,之前一直想用Distinct来得到指定字段不重复的记录是个误区。

以上这篇Django 解决distinct无法去除重复数据的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们