Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8360389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:27:10+00:00 2026-06-09T11:27:10+00:00

I have a bloomp (like a post), when I select it from the database,

  • 0

I have a “bloomp” (like a post), when I select it from the database, I SUM the number of comments, likes and if the current user liked.

The problem is with the number of likes and comments. For example:

If the bloomp has 3 likes and 1 comment, the select return that it has 3 likes and 3 comments, and vice-versa.

Here’s my query:

SELECT `bloomps`.`id` AS id,
       `bloomps`.`text` AS text,
       `bloomps`.`date` AS date,
       `images`.`id` AS image_id,
       `videos`.`id` AS video_id,
       `videos`.`provider` AS video_provider,
       `users`.`name` AS author_name,
       `users`.`picture` AS author_picture,
       SUM(CASE WHEN likes.bloomp_id IS NOT NULL THEN 1 ELSE 0 END) AS likes_number,
       SUM(CASE WHEN likes.bloomp_id IS NOT NULL AND likes.author_id = 1 THEN 1 ELSE 0 END) AS liked,
       SUM(CASE WHEN comments.bloomp_id IS NOT NULL THEN 1 ELSE 0 END) AS comments_number
FROM (`bloomps`)
INNER JOIN `users` ON `users`.`id` = `bloomps`.`author_id`
LEFT JOIN `likes` ON `likes`.`bloomp_id` = `bloomps`.`id`
LEFT JOIN `comments` ON `comments`.`bloomp_id` = `bloomps`.`id`
LEFT JOIN `images` ON `images`.`bloomp_id` = `bloomps`.`id`
LEFT JOIN `videos` ON `videos`.`bloomp_id` = `bloomps`.`id`
GROUP BY `bloomps`.`id`
ORDER BY `bloomps`.`id` ASC
LIMIT 10

enter image description here

enter image description here

enter image description here

This query:

SELECT `bloomps`.`id` AS id,
       `bloomps`.`text` AS text,
       `bloomps`.`date` AS date,
       `users`.`name` AS author_name,
       `users`.`picture` AS author_picture,
       SUM(CASE WHEN likes.bloomp_id IS NOT NULL THEN 1 ELSE 0 END) AS likes_number,
       SUM(CASE WHEN likes.bloomp_id IS NOT NULL AND likes.author_id = 1 THEN 1 ELSE 0 END) AS liked,
       SUM(CASE WHEN comments.bloomp_id IS NOT NULL THEN 1 ELSE 0 END) AS comments_number
FROM (`bloomps`)
INNER JOIN `users` ON `users`.`id` = `bloomps`.`author_id`
LEFT JOIN `likes` ON `likes`.`bloomp_id` = `bloomps`.`id`
LEFT JOIN `comments` ON `comments`.`bloomp_id` = `bloomps`.`id`
GROUP BY `bloomps`.`id`
ORDER BY `bloomps`.`id` ASC
LIMIT 10

Is returning:

Array
(
    [0] => Array
        (
            [id] => 1
            [text] => All these years I've been feeling like I was growing into myself. Finally, I feel grown.
            [date] => 0000-00-00 00:00:00
            [image_id] => 
            [video_id] => yfg8CMWeUAU
            [video_provider] => youtube
            [author_name] => Claudius Ibn
            [author_picture] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [likes_number] => 10
            [liked] => 5
            [comments_number] => 10
        )

    [1] => Array
        (
            [id] => 2
            [text] => Reveal not every secret you have to a friend, for how can you tell but that friend may hereafter become an enemy. And bring not all mischief you are able to upon an enemy, for he may one day become your friend.
            [date] => 0000-00-00 00:00:00
            [image_id] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [video_id] => 
            [video_provider] => 
            [author_name] => Elza Virginia
            [author_picture] => 77de68daecd823babbb58edb1c8e14d7106e83bb.jpg
            [likes_number] => 6
            [liked] => 6
            [comments_number] => 6
        )

    [2] => Array
        (
            [id] => 3
            [text] => To me, old age is always 15 years older than I am.
            [date] => 0000-00-00 00:00:00
            [image_id] => 41025046575248278_WzB3wKD0_f.jpg
            [video_id] => 
            [video_provider] => 
            [author_name] => Elza Virginia
            [author_picture] => 77de68daecd823babbb58edb1c8e14d7106e83bb.jpg
            [likes_number] => 1
            [liked] => 1
            [comments_number] => 0
        )

    [3] => Array
        (
            [id] => 4
            [text] => Accept the pain, cherish the joys, resolve the regrets; then can come the best of benedictions - 'If I had my life to live over, I'd do it all the same.
            [date] => 0000-00-00 00:00:00
            [image_id] => 
            [video_id] => 
            [video_provider] => 
            [author_name] => Claudio Cardozo
            [author_picture] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpg
            [likes_number] => 0
            [liked] => 0
            [comments_number] => 0
        )

    [4] => Array
        (
            [id] => 5
            [text] => Be master of your petty annoyances and conserve your energies for the big, worthwhile things. It isn't the mountain ahead that wears you out - it's the grain of sand in your shoe.
            [date] => 0000-00-00 00:00:00
            [image_id] => 4889470_700b.jpg
            [video_id] => 
            [video_provider] => 
            [author_name] => Claudius Ibn
            [author_picture] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [likes_number] => 1
            [liked] => 1
            [comments_number] => 0
        )

    [5] => Array
        (
            [id] => 6
            [text] => I think we're having fun. I think our customers really like our products. And we're always trying to do better.
            [date] => 0000-00-00 00:00:00
            [image_id] => 
            [video_id] => 
            [video_provider] => 
            [author_name] => Claudius Ibn
            [author_picture] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [likes_number] => 0
            [liked] => 0
            [comments_number] => 0
        )

    [6] => Array
        (
            [id] => 7
            [text] => [Addiction's] not about placating the bad dog - it's about feeding the good dog. You still have to feed the bad dog, but only enough so that the ASPCA doesn't bring you up on charges.
            [date] => 0000-00-00 00:00:00
            [image_id] => 
            [video_id] => 
            [video_provider] => 
            [author_name] => Claudius Ibn
            [author_picture] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [likes_number] => 0
            [liked] => 0
            [comments_number] => 0
        )

    [7] => Array
        (
            [id] => 8
            [text] => I've been on a diet for two weeks and all I've lost is two weeks.
            [date] => 0000-00-00 00:00:00
            [image_id] => 
            [video_id] => 
            [video_provider] => 
            [author_name] => Claudio Cardozo
            [author_picture] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpg
            [likes_number] => 1
            [liked] => 1
            [comments_number] => 0
        )

    [8] => Array
        (
            [id] => 9
            [text] => A book burrows into your life in a very profound way because the experience of reading is not passive.
            [date] => 0000-00-00 00:00:00
            [image_id] => 
            [video_id] => 
            [video_provider] => 
            [author_name] => Elza Virginia
            [author_picture] => 77de68daecd823babbb58edb1c8e14d7106e83bb.jpg
            [likes_number] => 1
            [liked] => 1
            [comments_number] => 0
        )

    [9] => Array
        (
            [id] => 10
            [text] => Touch your customer, and you're halfway there.
            [date] => 0000-00-00 00:00:00
            [image_id] => 4878040_460s_v1.jpg
            [video_id] => 
            [video_provider] => 
            [author_name] => Claudius Ibn
            [author_picture] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [likes_number] => 0
            [liked] => 0
            [comments_number] => 0
        )

    [10] => Array
        (
            [id] => 11
            [text] => I know starting careers in troubled times is a challenge, but it is also a privilege. Because it's moments like these that force us to try harder, dig deeper and to discover gifts we never knew we had. To find the greatness that lies within each of us. So don't ever shy away from that endeavor. Don't stop adding to your body of work. I can promise that you will be the better for that continued effort as will be this nation that we all love.
            [date] => 0000-00-00 00:00:00
            [image_id] => 20547742019404735_U0zJkj83_f.jpg
            [video_id] => 
            [video_provider] => 
            [author_name] => Elza Virginia
            [author_picture] => 77de68daecd823babbb58edb1c8e14d7106e83bb.jpg
            [likes_number] => 0
            [liked] => 0
            [comments_number] => 0
        )

    [11] => Array
        (
            [id] => 12
            [text] => Misery no longer loves company. Nowadays it insists on it.
            [date] => 0000-00-00 00:00:00
            [image_id] => 
            [video_id] => 
            [video_provider] => 
            [author_name] => Elza Virginia
            [author_picture] => 77de68daecd823babbb58edb1c8e14d7106e83bb.jpg
            [likes_number] => 0
            [liked] => 0
            [comments_number] => 1
        )

    [12] => Array
        (
            [id] => 13
            [text] => A good novel tells us the truth about its hero; but a bad novel tells us the truth about its author.
            [date] => 0000-00-00 00:00:00
            [image_id] => 4887752_460s.jpg
            [video_id] => 
            [video_provider] => 
            [author_name] => Elza Virginia
            [author_picture] => 77de68daecd823babbb58edb1c8e14d7106e83bb.jpg
            [likes_number] => 0
            [liked] => 0
            [comments_number] => 0
        )

    [13] => Array
        (
            [id] => 14
            [text] => Acting is the most minor of gifts and not a very high-class way to earn a living. After all, Shirley Temple could do it at the age of four.
            [date] => 0000-00-00 00:00:00
            [image_id] => 
            [video_id] => 
            [video_provider] => 
            [author_name] => Claudius Ibn
            [author_picture] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [likes_number] => 0
            [liked] => 0
            [comments_number] => 0
        )

    [14] => Array
        (
            [id] => 15
            [text] => The time to repair the roof is when the sun is shining.
            [date] => 0000-00-00 00:00:00
            [image_id] => 
            [video_id] => 
            [video_provider] => 
            [author_name] => Claudius Ibn
            [author_picture] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [likes_number] => 0
            [liked] => 0
            [comments_number] => 0
        )

    [15] => Array
        (
            [id] => 16
            [text] => I am doomed to an eternity of compulsive work. No set goal achieved satisfies. Success only breeds a new goal. The golden apple devoured has seeds. It is endless.
            [date] => 0000-00-00 00:00:00
            [image_id] => 
            [video_id] => 5431060
            [video_provider] => vimeo
            [author_name] => Claudio Cardozo
            [author_picture] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpg
            [likes_number] => 0
            [liked] => 0
            [comments_number] => 0
        )

    [16] => Array
        (
            [id] => 17
            [text] => In every American there is an air of incorrigible innocence, which seems to conceal a diabolical cunning.
            [date] => 0000-00-00 00:00:00
            [image_id] => image18.png
            [video_id] => 
            [video_provider] => 
            [author_name] => Claudius Ibn
            [author_picture] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [likes_number] => 1
            [liked] => 1
            [comments_number] => 0
        )

    [17] => Array
        (
            [id] => 18
            [text] => Everything should be made as simple as possible, but not one bit simpler.
            [date] => 0000-00-00 00:00:00
            [image_id] => 
            [video_id] => 
            [video_provider] => 
            [author_name] => Claudius Ibn
            [author_picture] => 356a192b7913b04c54574d18c28d46e6395428ab.jpg
            [likes_number] => 0
            [liked] => 0
            [comments_number] => 0
        )

    [18] => Array
        (
            [id] => 19
            [text] => When I only begin to read, I forget I'm on this world. It lifts me on wings with high thoughts.
            [date] => 0000-00-00 00:00:00
            [image_id] => 
            [video_id] => 
            [video_provider] => 
            [author_name] => Claudio Cardozo
            [author_picture] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpg
            [likes_number] => 0
            [liked] => 0
            [comments_number] => 0
        )

)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:27:11+00:00Added an answer on June 9, 2026 at 11:27 am

    You have joined more than one tables to the main (bloomps) table and all these have 1-to-many relationship with it. The result is a mini-cartesian join and wrong counts when grouping.

    You need to group each one separately in subqueries and then join these to the base table – and no grouping there. Try this:

    SELECT 
        b.id
      , b.*                                -- whatever column from `bloomps`
      , COALESCE(l.cnt,0)   AS likes_number,
      , COALESCE(l.liked,0) AS liked,
      , COALESCE(c.cnt,0)   AS comments_number
    FROM 
          bloomps AS b
       LEFT JOIN 
          ( SELECT bloomp_id
                 , COUNT(*) AS cnt 
                 , COUNT(CASE WHEN author_id = 1 THEN 1 END) AS liked
            FROM likes
            GROUP BY bloomp_id
          ) AS l
             ON l.bloomp_id = b.id
       LEFT JOIN 
          ( SELECT bloomp_id
                 , COUNT(*) AS cnt 
            FROM comments
            GROUP BY bloomp_id
          ) AS c
             ON c.bloomp_id = b.id
                                             -- no need for GROUP BY
    ORDER BY id ASC 
      LIMIT 10 ;
    

    If you don’t want subqueries, you could keep the query with the modifications below. Test which one is more efficient:

    SELECT 
        b.id
      , b.test
      , b.date 
      , COUNT(DISTINCT l.id)  AS likes_number,
      , COUNT(DISTINCT CASE WHEN l.author=1 THEN l.id END) AS liked,
      , COUNT(DISTINCT c.id)  AS comments_number
    FROM 
          bloomps AS b
       LEFT JOIN 
          likes AS l
             ON l.bloomp_id = b.id
       LEFT JOIN 
          comments AS c
             ON c.bloomp_id = b.id
    GROUP BY b.id ASC                  -- MySQL non-standard syntax for
                                       -- GROUP BY b.id 
                                       -- ORDER BY b.id ASC 
      LIMIT 10 ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have searched the database but need to specifically sum(of hours flown or days off)in
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
Have a painfully simple blog Post creator, and I'm trying to check if the
Have converted devise new session from erb to Haml but doens't work, this is
Have a look at one of my websites: moskah.com The problem is that it
have a php code like this,going to convert it in to C#. function isValid($n){
Have one Doubt In MVC Architecture we can able to pass data from Controller
I have placed the two bloom classes into my project from the Bloom sample
Have advancements in CPU design like dynamic instruction scheduling narrowed the performance gap between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.