sql >> Base de Datos >  >> RDS >> Mysql

MySQL con InnoDB:¿Cómo evitar usar COUNT?

Pon tus condiciones en un sum() . Así obtienes conteos individuales

SELECT SUM(sourceItem = 1 AND destinationItem = 1) AS count1,
       SUM(sourceItem = 1 AND destinationItem = 2) AS count2
FROM trades

Para obtener el conteo condicional completo, haz

SELECT SUM(case when sourceItem > 0 and destinationItem > 0 then 2
                when sourceItem > 0 or destinationItem > 0 then 1
                else 0 
           end) AS complete_sum
FROM trades

o

SELECT SUM(sourceItem > 0) + sum(destinationItem > 0) AS complete_sum
FROM trades