1

I have a table named 'blogDetails' having blogId, userId, blogTitle, blogContent I have another table named 'blogPopularity' having blogId, userId, popularityStat.

Now in the second table(blogPopularity) the field 'popularityStat' can contains either 'L' (for likes) or 'D' (for dislikes).

I am using Stored Procedure to insert both contents(to blogDetails) and like/dislike status(to blogPopularity).

I want to display the number of Likes and Dislikes for each blog.

Anybody suggest me how to do this.

1 Answer 1

4
SELECT  a.blogId, 
        a.blogTitle,
        SUM(CASE WHEN b.popularityStat = 'L' THEN 1 ELSE 0 END) `LIKES`,
        SUM(CASE WHEN b.popularityStat = 'D' THEN 1 ELSE 0 END) `DISLIKES`
FROM    blogDetails a
        LEFT JOIN blogPopularity b
            ON a.blogID = b.blogID
GROUP   BY  a.blogId, a.blogTitle

Sample Result

╔════════╦═══════════╦═══════╦══════════╗
║ BLOGID ║ BLOGTITLE ║ LIKES ║ DISLIKES ║
╠════════╬═══════════╬═══════╬══════════╣
║      1 ║ Title1    ║     7 ║        3 ║
║      2 ║ Title2    ║     6 ║        4 ║
║      3 ║ Title3    ║     3 ║        1 ║
╚════════╩═══════════╩═══════╩══════════╝
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.