此计数COUNT行为的说明

时间:2017-01-31 12:27:44

标签: mysql

我在理解这种行为时遇到了问题,有人可以向我解释一下吗?我想要检索的是所有父母用户ID,但没有他的孩子离开学校的父母 escolarity.baja = 1 。在这张表中" escolarity"该领域" baja"可能是'',NULL,0或1.这样的EMBEDDED选择部分" esc.baja!= 1"意味着用户没有离开学校,但这并不排除字段中的1个值" baja"但是,如果我设置" esc.baja!= 0"它仅计算baja = 1并仅在esc.baja = 1时返回字段。 为什么会这样?

    SELECT user.id
    FROM usuario as user 
    JOIN user_type as ut ON (ut.tipo=4 AND ut.id_usuario=user.id    ) 
    JOIN alumno_parent as prog ON prog.id_parent = user.id
    JOIN escolarity as esc ON prog.id_alumno = esc.id_alumno 
    WHERE 1 AND 
    # EMBEDDED select
        (SELECT COUNT(*) FROM alumno_parent as ap 
         JOIN user_type as ut1 ON (ut1.id_usuario=ap.id_alumno AND ut1.tipo = 1 ) 
         JOIN matric as mat ON (mat.id_alumno = ut1.id_usuario AND id_grupo = 464)
         JOIN escolarity as esc ON mat.id_alumno = esc.id_alumno   
         WHERE ap.id_parent = user.id AND esc.baja != 1 ) > 0 
    GROUP BY user.id ORDER BY user.apellido1, user.apellido2 , user.nombre;

0 个答案:

没有答案