[Solved] MYSQL get count of each column where it equals a specific value
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

MYSQL get count of each column where it equals a specific value

Database type:

I recently set up a MYSQL database connected to a form filled with checkboxes. If the checkbox was selected, it would insert into the associated column a value of '1'; otherwise, it would receive a value of '0'.

I'd like to eventually look at aggregate data from this form, and was wondering if there was any way I could use MYSQL to get a number for each column which would be equal to the number of rows that had a value of '1'.

I've tried variations of:

select count(*) from POLLDATA group by column_name

which was unsuccessful, and nothing else I can think of seems to make sense (admittedly, I'm not all too experienced in SQL).

I'd really like to avoid:

select count(*) from POLLDATA where column_1='1'

for each column (there close to 100 of them). Is there any way to do this besides typing out a select count(*) statement for each column?

EDIT:

If it helps, the columns are 'artist1', 'artist2', ....'artist88', 'gender', 'age', 'city', 'state'. As I tried to explain below, I was hoping that I'd be able to do something like:

select sum(EACH_COLUMN) from POLLDATA where gender='Male', city='New York City';

(obviously EACH_COLUMN is bogus)

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
Optimal indexes for this query:
ALTER TABLE `POLLDATA` ADD INDEX `polldata_idx_column_name` (`column_name`);
The optimized query:
SELECT
        count(*) 
    FROM
        POLLDATA 
    GROUP BY
        POLLDATA.column_name 
    ORDER BY
        NULL

Related Articles



* original question posted on StackOverflow here.