【问题标题】:how to execute subquery in Django orm?如何在 Django orm 中执行子查询?
【发布时间】:2014-11-17 13:51:34
【问题描述】:

django 模型.py

class cdr(models.Model):
    id = models.AutoField(primary_key=True, unique=True, verbose_name='id',)
    disposition = models.CharField(max_length=45, default='')
    did = models.CharField(max_length=50, default='')

    def __unicode__(self):
        return u'%s' % self.id

    class Meta:
        ordering=['-calldate']
        db_table = 'cdr'

MySQL 查询:

select id, did as diddst, count(did) as count, (select count(did) from cdr where disposition='NO ANSWER' and did=diddst) as countnoanswer from cdr where did in (79244576674, 79244576619) group by did;

结果

+------+-------------+-------+---------------+
| id   | diddst      | count | countnoanswer |
+------+-------------+-------+---------------+
| 1011 | 79244576619 |   218 |            71 |
| 1756 | 79244576674 |  1528 |           654 |
+------+-------------+-------+---------------+

如何在 Django orm 中执行这个子查询?帮帮我,请 Django 伙计们!

【问题讨论】:

    标签: python mysql django orm subquery


    【解决方案1】:

    您必须进行原始查询。

    cdr.objects.raw('...')
    

    【讨论】:

      猜你喜欢
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 2019-11-18
      • 2014-09-04
      • 1970-01-01
      • 2018-08-03
      相关资源
      最近更新 更多