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

  • Home
  • SEARCH
  • 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 3360940
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:04:42+00:00 2026-05-18T03:04:42+00:00

There is a hypothetical table called Table1, with the columns: id condition 1 condition

  • 0

There is a hypothetical table called Table1, with the columns:

  • id
  • condition 1
  • condition 2
  • joinable_key

There is another one, Main_Table, whose ID corresponds with the joinable_key in the first one.

I would like to join them in such a fashion that I can count the rows of Table1 according to both condition 1 and condition 2 separately – that is, I would like to be able to perform “count(Table1.condition1) as first_condition, count(Table1.condtion2) as second_condition” on the select query.

The query would presumably look something like this:

SELECT  Main_Table.some_column, COUNT(Table1.condition1) AS first_condition, COUNT(Table1.condtion2) AS second_condition
FROM  Main_Table
LEFT JOIN Table1 AS T1 on (T1.joinable_key = Main_Table.id AND T1.condition1 = 'something')
LEFT JOIN Table1 AS T2 on (T2.joinable_key = Main_Table.id AND T2.condition2 = 'something else')
GROUP BY (Main_Table.id)

When this executes, however, both count results are equal, and actually multiply with each other.
It is imperative that all results be included in the final output – including those that do not have any entries from Table1 – that is, if there is no row in Table1 with a joinable_key equal to Main_Table.id, it too needs to be included.

Before anyone suggest actually doing two separate queries and handling it through PHP – yes, I know why and how it can be done, but my goal is to find out whether or not this multi-count can be done all in one query.

Thank you

  • 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-18T03:04:43+00:00Added an answer on May 18, 2026 at 3:04 am
    SELECT some_column,
           (SELECT COUNT(1)
              FROM Table1
             WHERE mt.id = joinable_key
               AND condition1 = 'something'
           ) AS first_condition,
           (SELECT COUNT(1)
              FROM Table1
             WHERE mt.id = joinable_key
               AND condition2 = 'something else'
           ) AS second_condition
      FROM MainTable mt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are two popular closure styles in javascript. The first I call anonymous constructor
There is a field in my company's Contacts table. In that table, there is
There is a conversion process that is needed when migrating Visual Studio 2005 web
There are two weird operators in C#: the true operator the false operator If
There is previous little on the google on this subject other than people asking
There seem to be many ways to define singletons in Python. Is there a
There are a few ways to get class-like behavior in javascript, the most common
There are numerous Agile software development methods. Which ones have you used in practice
There are several types of objects in a system, and each has it's own
There are numerous libraries providing Linq capabilities to C# code interacting with a MySql

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.