【问题标题】:How to convert this SQL query to Django Queryset?如何将此 SQL 查询转换为 Django 查询集?
【发布时间】:2023-01-31 19:01:50
【问题描述】:

我有这个查询,它从两个不同的表中选择值,并在匹配的 ID 上使用数组 agg 如何使用查询集获得相同的结果。谢谢!

select
    sf.id_s2_users ,
    array_agg(sp.id)
from
    s2_followers sf
left join s2_post sp on
    sp.id_s2_users = sf.id_s2_users1
where
    sp.id_s2_post_status = 1
    and sf.id_s2_user_status = 1
group by
    sf.id_s2_users

【问题讨论】:

    标签: django postgresql django-queryset


    【解决方案1】:

    如果需要,您可以使用 Django 的 ORM 运行原始 SQL 查询。在这种情况下,您不必更改查询,您可以查看documentation here

    【讨论】:

      猜你喜欢
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多