当 table 引用 table 的不同行时如何多次加入 table

how to join a table more than once when a table references different rows of that table

我有一个 table,它最多可以引用另一个 table。

在我的 Django ORM 中

class CreatorCategories(models.Model):
    name = models.CharField(max_length=100, unique=True)
    icon = models.CharField(max_length=200, unique=True)

class ContentCreatorUsers(models.Model):
    creatorcategory1 = models.ForeignKey(CreatorCategories, related_name='creatorcategory1', on_delete=models.DO_NOTHING, null=True, blank=True)
    creatorcategory2 = models.ForeignKey(CreatorCategories, blank=True, related_name='creatorcategory2', null=True, on_delete=models.SET_NULL)
    creatorcategory3 = models.ForeignKey(CreatorCategories, blank=True, null=True, related_name='creatorcategory3', on_delete=models.SET_NULL)
    topbannerphotokey = models.CharField(max_length=100, null=True, blank=True)

我是 运行 aws lambda 中的 sql 查询(我没有添加 select 语句来获取类别名称和图标的值)

SELECT id, creatordisplayname, contentcreatordisplaycardurl, creatorurlslug,
    currentmonitaryactioncount, isverifiedcontentcreator
    FROM users_contentcreatorusers
    INNER JOIN users_creatorcategories ON users_contentcreatorusers.creatorcategory1_id= users_creatorcategories.id
    INNER JOIN users_creatorcategories ON ISNULL(users_contentcreatorusers.creatorcategory2_id, NULL) = ISNULL(users_creatorcategories.id, NULL)
    INNER JOIN users_creatorcategories ON ISNULL(users_contentcreatorusers.creatorcategory3_id, NULL) = ISNULL(users_creatorcategories.id, NULL)
    WHERE approvedcreator = true
     ORDER BY currentmonitaryactioncount DESC
     LIMIT 12;
     

这个 sql 查询一直给我同样的错误:

Traceback (most recent call last):
  File "browsecontentcreatorstest.py", line 115, in <module>
    lambda_handler()
  File "browsecontentcreatorstest.py", line 57, in lambda_handler
    cur.execute('''
psycopg2.errors.DuplicateAlias: table name "users_creatorcategories" specified more than once

我需要能够获取创作者拥有的每个类别的名称和图标。 我不确定如何在没有连接的情况下执行此操作。到目前为止我看过的文档没有解释如何清理它。

使用别名来消除引用的歧义:

FROM users_contentcreatorusers AS uccu
INNER JOIN users_creatorcategories AS ucc1 ON uccu.creatorcategory1_id = ucc1.id
INNER JOIN users_creatorcategories AS ucc2 ON ISNULL(uccu.creatorcategory2_id, NULL) = ISNULL(ucc2.id, NULL)
INNER JOIN users_creatorcategories AS ucc3 ON ISNULL(uccu.creatorcategory3_id, NULL) = ISNULL(ucc3.id, NULL)