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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:24:03+00:00 2026-05-25T02:24:03+00:00

I am in a position where I want multiple counts from a single table

  • 0

I am in a position where I want multiple counts from a single table based on different combination of conditions.

The table has 2 flags: A & B.

I want count for following criteria on same page:

  1. A is true (Don’t care about B)
  2. A is false (Don’t care about B)
  3. A is true AND B is true
  4. A is false AND B is true
  5. A is true AND B is false
  6. A is false AND B is false
  7. B is true (Don’t care about A)
  8. B is false (Don’t care about A)

I want all above count on same page. Which of following will a good approach for this:

  1. Query for count on that table for each condition. [That is firing 8 queries every time user gives the command.]
  2. Query for list of data from database and then count values for appropriate conditions on UI.

Which option should I choose? Do you know any other alternative for this?

  • 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-25T02:24:03+00:00Added an answer on May 25, 2026 at 2:24 am

    Your table essentially looks like this (The ID column is redundant, but I expect you have other data in your actual table anyway.):

    CREATE TABLE `stuff` (
        `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
        `a` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
        `b` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
        PRIMARY KEY (`id`)
    )
    

    Some sample data:

    INSERT INTO `stuff` (`id`, `a`, `b`) VALUES (1, 0, 0);
    INSERT INTO `stuff` (`id`, `a`, `b`) VALUES (2, 0, 1);
    INSERT INTO `stuff` (`id`, `a`, `b`) VALUES (3, 1, 0);
    INSERT INTO `stuff` (`id`, `a`, `b`) VALUES (4, 1, 1);
    

    This query (in mysql, I’m not sure about other DBMS) should produce the results you want.

    select
    count(if (a = 1,             1, NULL)) as one,
    count(if (a = 0,             1, NULL)) as two,
    count(if (a = 1 && b = 1,    1, NULL)) as three,
    count(if (a = 0 && b = 1,    1, NULL)) as four,
    count(if (a = 1 && b = 0,    1, NULL)) as five,
    count(if (a = 0 && b = 0,    1, NULL)) as six,
    count(if (b = 1,             1, NULL)) as seven,
    count(if (b = 0,             1, NULL)) as eight
    from stuff
    group by null
    

    With the sample, simple data above, the query generates:

    one, two, three, four, five, six, seven, eight
    2  , 2  , 1,     1,    1,    1,   2,     2
    

    Notes:

    group by null
    

    This just causes every row ro be in the group.

    count(...)
    

    This function counts all the NON null values in the group, which is why we use the if(…) to return null if the condition is not met.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing multiple addresses, including their Lat/Long coordinates, and I want
I have an iframe which has a table structure with multiple rows and columns.
I want to write multiple structures to a single file. I used fwrite function
I want to make a view move to a different position on window resize.
I want to position an element in normal css relative to the element before
I want to position Dojo's Dijit Dialog relative to one of my html element.
Let's say I want to position an element at the coordinates x=20, y=5 within
In Autohotkey, I want to position my Gui window to right bottom of the
I want to write a raw byte/byte stream to a position in a file.
I want to get hold of the mouse position in a timeout callback. As

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.