根据countries.haveState选择instituteId的计数。如果haveState = 1那么,Count(institutionss.instituteid)基于stateId,如if(institution.stateId =' 1')然后计数Count(institutions.instituteid),否则计算没有stateId的instituteId,如下所示。
SELECT countries.countryid,
Count(institutions.instituteid) AS instituteCount
FROM countries
RIGHT JOIN institutions
ON countries.countryid = institutions.countryid
WHERE countries.countryid = '32'
AND countries.status = '1'