在数据库中查找特定字段的重复条目 [英] Find duplicate entries in database for specific fields

查看:65
本文介绍了在数据库中查找特定字段的重复条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,其中包含姓名,学校,电子邮件,地址等字段.

I have model having fields as name, school, email, address etc.

我想找出在 name school 字段中具有相同条目的行?

I want to find out the rows for which there is same entry for name and school field ?

eg:
name school email 
abc     mps    abc@gmail.com
abc     mps    abc@gmail.com
xyz     vps    xyz@gmail.com
abc     mps    abc@gmail.com
poi     vps    poi@gmail.com
jkl     vps    jkl@gmail.com

name abc和 school mps一样,mps是3个条目,而xyz和vps有2个条目我可以嵌套 for 循环和迭代方式,以对照表中的所有行检查名称和学校字段.那将是性能下降(n * n)

like for name abc and school mps ther are 3 entries and for xyz and vps there are 2 entries I can nested for loop and iterative manner to check the name and school fields against all the rows in the table. That would be performance hit (n*n)

还有其他方法可以找到吗?

Is there any other way to find out ?

推荐答案

distinct() will serve the purpose. The following will solve your problem

model_name.objects.values_list('name').distinct()

如果您想要字典,则必须这样做

If you want dictionary then you have to do this

model_name.objects.values('name').distinct()

希望能解决您的问题!

这篇关于在数据库中查找特定字段的重复条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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