计数,加入或可能是GROUP By

时间:2012-12-27 17:23:30

标签: sql join count group-by

所以我做了一个简单的查询,给出了一些案例的总和,其中提到了PassportNumber ='C_AA8'。但是如果我设置了在RegistrationCase中不存在的PassportNumber的查询,它将返回0(据我所知它不能JOIN)。 我该如何解决?还有一种方法可以重新编译此查询以使用GROUP BY吗? 感谢您的任何进一步帮助。 我正在使用PHP与PHPmyadmin和AMPPS。固定寄存器。

SELECT COUNT( unRegisterCase.PassportNumber ) + COUNT( RegistrationCase.PassportNumber ) + COUNT( RecoveringCase.NewPassportNumber ) AS result
FROM RegistrationCase
LEFT JOIN unRegisterCase ON unRegisterCase.PassportNumber = RegistrationCase.Passportnumber
LEFT JOIN RecoveringCase ON RecoveringCase.NewPassportNumber = RegistrationCase.Passportnumber
WHERE RegistrationCase.PassportNumber =  'C_AA8'

2 个答案:

答案 0 :(得分:1)

不是吗?

SELECT COUNT(PassportNumber ) as PassportCount FROM
(
SELECT PassportNumber FROM RegistrationCase UNION ALL
SELECT NewPassportNumber FROM RecoveringCase UNION ALL
SELECT PassportNumber FROM unRegisterCaseber
) A
WHERE PassportNumber =  'C_AA8'

答案 1 :(得分:0)

与SQl底部的where子句一起取消左连接,

试试这个

 SELECT COUNT( UnregisterCase.PassportNumber ) + 
        COUNT( RegistrationCase.PassportNumber ) + 
        COUNT( RecoveringCase.NewPassportNumber ) AS result
 FROM RegistrationCase, unRegisterCase, RecoveringCase
 WHERE UnregisterCase.PassportNumber = RegistrationCase.Passportnumber
 AND RecoveringCase.NewPassportNumber = RegistrationCase.Passportnumber
 AND RegistrationCase.PassportNumber =  'C_AA8'

同时检查区分大小写,因为您有不同的unRegister和Unregister

拼写