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

Cómo incrementar el recuento de ocurrencias del valor de la columna en MySQL

Puede usar variables en versiones anteriores de MySQL:

select t.*,
       (@rn := if(@ce = customer_email, @rn + 1,
                  if(@ce := customer_email, 1, 1)
                 )
       ) as occurrences
from (select t.*
      from t
      order by customer_email, created_at
     ) t cross join
     (select @ce := '', @rn := 0) params;

En MyQL 8+, recomendaría row_number() :

select t.*,
       row_number() over (partition by customer_email order by created_at) as occurrences
from t;