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.
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 ║
╚════════╩═══════════╩═══════╩══════════╝
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.