在django中分组创建错误的查询

时间:2017-10-11 10:15:08

标签: python mysql django

我们在django

中写下了这个查询
ManageInterview.objects.filter
(post_id=request.data['job_id'])
.order_by('-id')
.annotate(total=Count('pitcher_id'))

打印完成后,我们得到了这个查询

SELECT *, COUNT(`pitcher_invitations`.`pitcher_id`) AS `total` FROM 
`pitcher_invitations` WHERE `pitcher_invitations`.`post_id` = 254555444
GROUP BY `pitcher_invitations`.`id` 
ORDER BY `pitcher_invitations`.`id` DESC

我们正在进行小组讨论

  

pitcher_id

但是django查询组由

组成
  

pitcher_invitationsid

我们想要这个查询

select * from `pitcher_invitations` where `post_id` =254555444
group by `pitcher_id` order by `id` desc  

2 个答案:

答案 0 :(得分:3)

试试这个:

query ="select * from `pitcher_invitations` where `post_id` = "+str(request.data['job_id'])+" group by `pitcher_id` order by `id` desc"

result = ManageInterview.objects.raw(query)

如果有人有好的解决方案,请分享

答案 1 :(得分:1)

您无法选择*和group_by。如果您想分组<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="button" class="Alan_button_blue" style="padding:50px;background:lightgray;margin-bottom:50vh;">Make an Offer</div> <div id="section5" style="width:100%;height:50vh;background:#aaafff"></div> <div id="instalments" class="Alan_button_2" style="padding:50px;background:darkblue;color:white;margin-bottom:50vh;">Instalments</div> <div id="section6" style="width:100%;height:50vh;background:#333333"></div> <div style="height:75vh;"></div>并计算您不需要提供*的记录,可以使用以下命令:

pitcher_id