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 6921877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:20:00+00:00 2026-05-27T10:20:00+00:00

Evening folks, I have a complex MySQL COUNT query I am trying to perform

  • 0

Evening folks,

I have a complex MySQL COUNT query I am trying to perform and am looking for the best way to do it.

  • In our system, we have References. Each Reference can have many (or no) Income Sources, each of which can be validated or not (status). We have a Reference table and an Income table – each row in the Income table points back to Reference with reference_id
  • On our ‘Awaiting’ page (the screen that shows each Income that is yet to be validated), we show it grouped by Reference. So you may, for example, see Mr John Smith has 3 Income Sources.
  • We want it to show something like “2 of 3 Validated” beside each row
  • My problem is writing the query that figures this out!

What I have been trying to do is this, using a combination of PHP and MySQL to bridge the gap where SQL (or my knowledge) falls short:

First, select a COUNT of the number of incomes associated with each reference:

SELECT `reference_id`, COUNT(status) AS status_count
FROM (`income`)
WHERE `income`.`status` =  0
GROUP BY `reference_id` 

Next, having used PHP to generate a WHERE IN clause, proceed to COUNT the number of confirmed references from these:

SELECT `reference_id`, COUNT(status) AS status_count
FROM (`income`)
WHERE `reference_id` IN ('8469', '78969', '126613', ..... etc 
AND status = 1
GROUP BY `reference_id` 

However this doesn’t work. It returns 0 rows.

Any way to achieve what I’m after?

Thanks!

  • 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-05-27T10:20:01+00:00Added an answer on May 27, 2026 at 10:20 am

    In MySQL, you can SUM() on a boolean expression to get a count of the rows where that expression is true. You can do this because MySQL treats true as the integer 1 and false as the integer 0.

    SELECT `reference_id`, 
      SUM(`status` = 1) AS `validated_count`, 
      COUNT(*) AS `total_count` 
    FROM `income`
    GROUP BY `reference_id` 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening. For a project, I have to create a system. In this system,
Evening all. I have the following code that I need looking into - basically
Good evening, here is what I am trying to achieve, I currently have a
Evening folks!! Stuck with this rather dull problem. I have deployed my website on
Evening, I came across this very useful post and I have everything looking good
Evening all - Whats the best way to access an image's height and width
Good evening, In my app that I'm currently developing, I have a class that
Last evening I did some housekeeping on our code repository - basically moved the
Good evening guys, I'm trying to pass multiple checkbox values through AJAX and process
Good evening, i need to know how can i have one .h file that

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.