当我执行此查询集时:
Club.objects.values('district').distinct()
我希望得到这个SQL:
SELECT DISTINCT `club_club`.`district` FROM `club_club` ORDER BY `club_club`.`name` ASC
但Django会生成此查询(添加'name'列):
>>> print Club.objects.values('district').distinct().query
SELECT DISTINCT `club_club`.`district`, `club_club`.`name` FROM `club_club` ORDER BY `club_club`.`name` ASC
有什么问题?
答案 0 :(得分:0)
如果您使用的是order by column
,那么您希望无效 select list
的查询必须在distinct keyword
中,否则您将收到此标准SQL错误< / p>
Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.