gpt4 book ai didi

mysql - 将 GROUP BY 添加到 Django 查询

转载 作者:行者123 更新时间:2023-11-29 00:39:57 25 4
gpt4 key购买 nike

我有以下查询:

titles = Title.objects.all()

在某些情况下,我需要为此查询获取 DISTINCT ('title')。 Django 在使用 MySQL 后端时不允许 distinct('title')

本质上,我想做的是:

titles = titles.extra('GROUP BY title')

我该如何正确执行此操作?

最佳答案

您是否尝试过使用 .distinct() 方法?

titles = Title.objects.values('title','other_columns_you_care_about_and_not_distinct').distinct()

如果您在 .all() 上执行 .distinct(),您将返回与开始时一样多的行,当然前提是您的 id 列是独特的。因此,您只需在 .values() 中拉回您需要的列。

关于mysql - 将 GROUP BY 添加到 Django 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12681018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com