【问题标题】:Sql select count to python codesql选择计数到python代码
【发布时间】:2014-06-14 12:34:45
【问题描述】:

我想知道是否有可能在 Python 中执行此选择的功能,我想知道更知名的诊断,但它可以在 3 个不同的领域,我知道此选择有效,但是我不知道如何将它放入 Python 中,或者是否还有其他方法可以在 Python 中做同样的事情。

SELECT Count(*)
 FROM   (
    SELECT DISTINCT
           imp_diagnostica_99
         , imp_diagnostica_100
         , imp_diagnostica_101
    FROM   expmedico_expedienteconsultainicial
   ) As distinctified

我想获得医生发送的更多程序。

模型.py

class AutorizacionImagenologia(models.Model):

imagenologia_fecha_solicitud_1 = models.DateField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateField(auto_now=True)
imagenologia_fecha_emision = models.DateField(auto_now=True, null=True, blank=True)
tipo_plan = models.CharField(max_length=60, null=True, blank=True)
empresa_empleadora = models.CharField(max_length=100, null=True, blank=True)
imagenologia_hora_de_solicitud_2 = models.TimeField(auto_now=True, null=True, blank=True)
imagenologia_medico_solicita_3 =  models.ForeignKey(Medico, related_name='MedicoImagenologia', null=True)
imagenologia_nombre_pac_4 = models.CharField(max_length=60, null=False, blank=True)
imagenologia_apellido =  models.CharField(max_length=60, null=False, blank=True)
imagenologia_fecha_nacimiento_5 = models.DateField(max_length=10, null=True, blank=True)
imagenologia_edad_miembro6 = models.CharField(max_length=10, null=True, blank=True)
imagenologia_sexo_7 = models.CharField(max_length=10, null=True, blank=True)
imagenologia_diagnostico_9 = models.CharField(max_length=100, null=True, blank=True)
imagenologia_credencial = models.CharField(max_length=20, null=False)
imagenologia_procedimiento = models.ForeignKey(
        Estudios_Imagenologia,
        related_name='ProcedimientosImg', blank=True, null=True)
imagenologia_procedimiento2 = models.ForeignKey(Estudios_Imagenologia, related_name='ProcedimientosImg2', blank=True, null=True)
imagenologia_procedimiento3 =  models.ForeignKey(
        Estudios_Imagenologia, related_name='ProcedimientosImg3',
        blank=True, null=True)

【问题讨论】:

    标签: python sql django


    【解决方案1】:

    您始终可以将 RawQuerySet 与自定义模型一起使用(仅用于此 sql),或使用连接游标 API:

    from django.db import connection
    
    with connection.cursor() as cursor:
        cursor.execute('your_sql_here')
        for row in cursor.fetchall():
            print 'row fields:', row[0], row[1], row[3]
    

    【讨论】:

      【解决方案2】:

      如果您使用 Django 并且有一个模型 expmedico_expedienteconsultainicial,您可以区分 expmedico_expedienteconsultainicial 对象:

      expmedico_expedienteconsultainicial.objects.all().values('imp_diagnostica_99', 'imp_diagnostica_100','imp_diagnostica_101').distinct().count()
      

      请展示您的 Django 模型并说明您想对结果做什么,以便我们为您提供最佳答案。

      Django 使用查询集的文档https://docs.djangoproject.com/en/dev/ref/models/querysets/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-30
        • 2019-01-31
        相关资源
        最近更新 更多