How to do two Select

0

I want to select the amount of like and deslike of each post but I'm not getting the deslike, can anyone help me?

    
asked by anonymous 20.03.2018 / 02:32

2 answers

2

Try this:

SELECT SUM(likes), SUM(deslikes), id_post FROM statusposts GROUP BY id_post;
    
20.03.2018 / 02:36
1

You should use COUNT(*) when you want to know the amount of tuples that result from your query, for example, if you want to know how many tuples exist in a table X just do SELECT COUNT(*) FROM X .

What you really want is to know how many likes and how many posts a post has received and, from what I understand, there are two columns of type bit , one like and one deslike that tell you if a given post received a like or a deslike of a particular user.

So you can add the likes and disks columns instead of counting, so you can use CASE WHEN to be able to convert bit to int 1 or 0 and sum them. Like the answer in the 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
    
20.03.2018 / 03:01