How to make two Select

Asked

Viewed 40 times

0

I want to select the amount of like and slide of each post but am not getting the slide, someone can help me? inserir a descrição da imagem aqui inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

2 answers

2


Try this:

SELECT SUM(likes), SUM(deslikes), id_post FROM statusposts GROUP BY id_post;
  • Nuss, how did I not fall into it! WTF !! vlw ABÇ bro

  • If you have removed your doubts close the question by accepting my answer

1

You must use it COUNT(*) when you want to know the number of tuples that result from your query, for example, if you want to know how many tuples there are in an X table just do SELECT COUNT(*) FROM X.

What you really want is to know how many Ikes and how many Deslikes a post received and, from what I understand, there are two columns of the kind bit, a like and another slide telling you if a particular post has received a like or a slide from a particular user.

Therefore, Voce can add the columns Likes and slide instead of counting, for that Voce can use a CASE WHEN in order to convert the bit in int 1 or 0 and add them up. As in the answer on link.

SELECT 
    SUM(CASE WHEN likes = 1 THEN 1 ELSE 0 END) AS quantlikes,
    SUM(CASE WHEN deslikes = 1 THEN 1 ELSE 0 END) AS quantdeslikes,
    id_post
FROM statuspost
WHERE id_grupo = 70
GROUP BY id_post
  • Because the CASE within the SUM()? The values are already 1 and 0

Browser other questions tagged

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