0
I need to count the number of tables records cliques
and curtidas
and show these numbers to the media type Video
table tipo_midia
and the specific media, that is, the video that the person liked or clicked, from the table videos
.
videos
+--+----------+--------------+
|id| titulo |id_tipo_midia |
+-------------+--------------+
|01| video_01 | 01 |
|02| video_02 | 01 |
|03| video_03 | 01 |
|04| video_04 | 01 |
+-------------+--------------+
cliques
+--------------+--------+------------+
|id_tipo_midia |id_midia| data_acesso|
+--------------+--------+------------+
|01 | 03 | 2019-06-03 |
|01 | 03 | 2019-06-03 |
|03 | 01 | 2019-06-05 |
+--------------+--------+------------+
curtidas
+--------------+--------+------------+
|id_tipo_midia |id_midia| data_acesso|
+--------------+--------+------------+
|01 | 03 | 2019-06-03 |
|01 | 03 | 2019-06-03 |
|01 | 01 | 2019-06-05 |
+--------------+--------+------------+
tipo_midia
+--+-------+
|id| tipo |
+----------+
|01| Vídeo |
|02| Audio |
|03| Imagem|
+----------+
My query:
select tipo_midia.tipo, videos.titulo, count(cliques.id_midia), count(curtidas.id_midia) from tipo_midia
left join videos on (tipo_midia.id = videos.id_tipo_midia)
left join cliques on (videos.id = cliques.id_midia)
left join curtidas on (videos.id = curtidas.id_midia)
where tipo_midia.id = '1'
group by 1, 2
The query must return me:
*---------*---------*--------*---------*
|tipoMidia| titulo | cliques| curtidas|
*---------*---------*--------*---------*
|video |video_03 | 2 | 2 |
|video |video_01 | 1 | 0 |
*---------*---------*--------*---------*
You know because in this case you can’t use the Count() function more than once without using subquerys?
– Jfé
is an aggregation function that counts the number of query result rows that follow a condition. If you need to count lines with different conditions, it has to be in two (or more) queries; hence the subquery. Read: http://www.postgresqltutorial.com/postgresql-count-function/
– Rovann Linhalis