Logging

Asked

Viewed 32 times

2

Good afternoon everyone, I have the following table: inserir a descrição da imagem aqui

I have the following select:

"SELECT DISTINCT `ip` FROM `visualizacoes` WHERE MONTH(data) = MONTH(NOW()) AND id_usuario = :id_usuario"

Use the distinct to returns only one record per ip, but would like to return an ip record per news.

EX: if a particular ip views the post 1, and then views the post 2 I want to return the two record.

But if it views the post 1 twice I want it to return only one record

PS: Ignore the existence of two columns on date.

  • 4

    Of the one group by ip, id_noticia see if it is the desired result, and take the distinct.

  • 1

    That’s exactly what I needed!

1 answer

4


As @rray already quoted in the comments, use GROUP BY.

SELECT ip, id_noticia
  FROM visualizacoes
 WHERE MONTH(data) = MONTH(NOW()) 
   AND id_usuario = :id_usuario
 GROUP BY ip, id_noticia;

With GROUP BY you can use aggregation functions. For example, if you want to count how many times each IP visited a particular news item, use the function COUNT:

SELECT ip, id_noticia, COUNT(*) AS visitas
  FROM visualizacoes
 WHERE MONTH(data) = MONTH(NOW()) 
   AND id_usuario = :id_usuario
 GROUP BY ip, id_noticia;
  • 1

    Thank you very much friend, both worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.