在数据库中查找特定字段的重复条目 [英] Find duplicate entries in database for specific fields
问题描述
我有一个模型,其中包含姓名,学校,电子邮件,地址等字段.
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屋!