Django在原始SQL查询中放置WHERE

时间:2016-06-17 13:37:46

标签: sql django where rawsql

此查询中WHERE的顺序错误,但现在是正确的。

postModel = list(PostModel.objects.raw(
SELECT max(pub_date), 
   count(topic_id) AS freq, 
   count(DISTINCT author) AS contributors 
   FROM crudapp_postmodel 
   WHERE author = "art" 
   GROUP BY topic_id 
   ORDER BY pub_date DESC
))

由于

1 个答案:

答案 0 :(得分:2)

简单:

SELECT max(pub_date), 
       count(topic_id) AS freq, 
       count(DISTINCT author) AS contributors 
       FROM crudapp_postmodel 
       WHERE author = "art" 
       GROUP BY topic_id 
       ORDER BY pub_date DESC