首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多对多django sql

多对多django sql
EN

Stack Overflow用户
提问于 2013-04-06 03:48:29
回答 2查看 2.1K关注 0票数 1

我有一个模型用户配置文件,它与另一个名为skill的表有许多关系,我还有一个模型组,它与另一个名为skill的表有许多关系

我想要进行一个查询,该查询将计算用户配置文件与每个组共有的技能数量。

我也想做一个查询,它会计算我的特定用户配置文件与其他用户配置文件中具有的技能数量

有人能帮我做这个吗。谢谢

模型:

代码语言:javascript
复制
class UserProfile(models.Model):    
    slug = models.SlugField(max_length=200)
    user = models.ForeignKey(User, unique =True)
    skills = models.ManyToManyField(Skills,null=True, blank=True)
    courses = models.ManyToManyField(Course,null=True, blank=True)

class Group(models.Model):
    slug = models.SlugField(max_length=200)
    name = models.CharField(max_length=200)
    skills = models.ManyToManyField(Skills,null=True, blank=True)

class Skills(models.Model):
    slug = models.SlugField(max_length=200)
    name = models.CharField(max_length=200)

所以我找到了一种为小组做这件事的方法

代码语言:javascript
复制
groupRecommendation = Group.objects.extra(
select={
    'skills_count': """
     SELECT COUNT(*) FROM axiom_alto_userprofile_skills
     JOIN axiom_alto_group_skills on axiom_alto_userprofile_skills.skills_id = axiom_alto_group_skills.skills_id
     WHERE axiom_alto_group_skills.group_id= axiom_alto_group.id AND axiom_alto_userprofile_skills.userprofile_id = %d """ % profile.id,

},

).order_by('-skills_count')

但是我不知道如何在用户之间做到这点

EN

回答 2

Stack Overflow用户

发布于 2013-04-06 17:10:46

您的第一个查询是针对特定的用户配置文件,计算用户与每个组共享的技能数量。在你的文章中,你展示了一种使用子查询来实现这一点的方法。但是,在某些数据库中,子查询的性能比连接差得多,因此您可能有兴趣了解如何使用连接而不是子查询来获得相同的结果:

代码语言:javascript
复制
SELECT G.id, G.slug, G.name, COUNT(GS.id) AS skills_count
FROM axiom_alto_group G
INNER JOIN axiom_alto_userprofile_skills US
  ON US.userprofile_id = %s
LEFT OUTER JOIN axiom_alto_group_skills GS
  ON GS.group_id = G.id AND US.skills_id = GS.skills_id
GROUP BY G.id, G.slug, G.name
ORDER BY skills_count DESC

在Django中,您可以在Group模型上使用raw SQL query运行:

代码语言:javascript
复制
Group.objects.raw(''' ... SQL as above ... ''', [profile.id])

现在应该很容易看到如何执行第二个查询了。也就是说,对于特定的用户配置文件,计算用户与每个其他用户共享的技能数量:

代码语言:javascript
复制
SELECT U.id, U.slug, U.user, COUNT(US2.id) AS skills_count
FROM axiom_alto_userprofile U
INNER JOIN axiom_alto_userprofile_skills US1
  ON US1.userprofile_id = %s
LEFT OUTER JOIN axiom_alto_userprofile_skills US2
  ON US2.userprofile_id = U.id AND US2.skills_id = US1.id
GROUP BY U.id, U.slug, U.user
ORDER BY skills_count DESC

同样,在Django中,您可以使用原始的SQL查询来运行它,这次是在UserProfile模型上:

代码语言:javascript
复制
UserProfile.objects.raw(''' ... SQL as above ... ''', [profile.id])
票数 1
EN

Stack Overflow用户

发布于 2013-04-07 02:36:00

代码语言:javascript
复制
groupRecommendation = Group.objects.extra(
    select={
    'skills_count': """
     SELECT COUNT(*) FROM axiom_alto_userprofile_skills
     JOIN axiom_alto_group_skills on axiom_alto_userprofile_skills.skills_id = axiom_alto_group_skills.skills_id
     WHERE axiom_alto_group_skills.group_id= axiom_alto_group.id AND axiom_alto_userprofile_skills.userprofile_id = %d """ % profile.id,
    },
).order_by('-skills_count')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15842326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档