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 帮帮我!
最佳答案
您必须执行原始查询。
cdr.objects.raw('...')
关于python - 如何在Django orm中执行子查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26974181/