Filtering records

2

Good afternoon guys, I have the following the following table:

Ihavethefollowingselect:

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

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

EX: If a given ip views post 1, and then view post 2 I want to return both records.

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

PS: Ignore the existence of two columns on date.

    
asked by anonymous 24.06.2016 / 20:56

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 aggregate functions . For example, if you want to count how many times each IP visited a particular news, use the 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;
    
24.06.2016 / 21:17