2015-10-23 37 views
0

我瞭解如何使用計數與分組,但是我需要一個特定的使用這種反向的,我嘗試做這個查詢:Mysql的次數羣通過反向

SELECT 
     COUNT(pac.strid_paciente) 
    FROM tb_paciente AS pac 
    LEFT JOIN tb_entidades AS ent 
     ON pac.strid_entidad = ent.strid_entidad 
    LEFT JOIN tb_escolaridad AS esc 
     ON pac.strid_escolaridad = esc.strID_Escolaridad 
    LEFT JOIN tb_etnia AS etn 
     ON pac.strid_etnia = etn.strID_Etnia 
    LEFT JOIN tb_raza AS raza 
     ON pac.strid_raza = raza.strid_raza 
    LEFT JOIN tb_religion AS rel 
     ON pac.strid_religion = rel.strid_religion 
    LEFT JOIN tb_ocupacion AS ocu 
     ON pac.strOfi_Paciente = ocu.strID_Ocupacion 
    LEFT JOIN tb_consulta AS con 
     ON pac.strid_paciente = con.strid_paciente 
    LEFT JOIN tb_usuarios AS usu 
     ON usu.strid_usuario = con.strid_medico 
    GROUP BY pac.strid_paciente 

,這讓我:

COUNT(pac.strid_paciente) 
1 
1 
2 
3 
4 
5 
2 

我需要每個計數的結果:18,問題是我不能通過因爲給出不同的結果而刪除組。類似的東西:

COUNT(pac.strid_paciente) 
    18 
+2

不清楚你在問什麼? – Sebas

+0

爲什麼你的需要,如果該組你只是返回計數?我同意@Sebas – GolezTrol

+0

我需要這個組,因爲沒有它'SELECT'會返回更多的行。 – user3267098

回答

0

你試過SUM(COUNT(pac.strid_paciente))或者如果失敗,那麼下面的工作:

SELECT SUM(t.cnt) FROM(
SELECT 
    COUNT(pac.strid_paciente) AS cnt 
FROM tb_paciente AS pac 
LEFT JOIN tb_entidades AS ent 
    ON pac.strid_entidad = ent.strid_entidad 
LEFT JOIN tb_escolaridad AS esc 
    ON pac.strid_escolaridad = esc.strID_Escolaridad 
LEFT JOIN tb_etnia AS etn 
    ON pac.strid_etnia = etn.strID_Etnia 
LEFT JOIN tb_raza AS raza 
    ON pac.strid_raza = raza.strid_raza 
LEFT JOIN tb_religion AS rel 
    ON pac.strid_religion = rel.strid_religion 
LEFT JOIN tb_ocupacion AS ocu 
    ON pac.strOfi_Paciente = ocu.strID_Ocupacion 
LEFT JOIN tb_consulta AS con 
    ON pac.strid_paciente = con.strid_paciente 
LEFT JOIN tb_usuarios AS usu 
    ON usu.strid_usuario = con.strid_medico 
GROUP BY pac.strid_paciente 
) AS t