Harikris
Harikris

Reputation: 310

I need a MySQL query for find the total count from a table

I need to find the total number of occurrence of a specific values in columns.

eg:

In the below image of my mysql table structure i have fields like essc_a1, essc_a2 ... essc_a10.

a1 to a10 entries coming from a group of ten questions.I need to find the number of occurrence of a value (for eg: "eg" or "S") in a1 to a10 columns.

I tried the following query but not working for me.

SELECT 
    COUNT(essc_a1,essc_a2,...,essc_a10) as count 
FROM 
    employee_satisfaction_survey_checklist 
GROUP BY 
    essc_a1, essc_a2,..., essc_a10 
WHERE
    essc_a1 LIKE '% es %' 
    OR essc_a2 LIKE '% es %' 
    OR .... essc_a10 LIKE '% es %';

Please help me.

enter image description here

Upvotes: 0

Views: 73

Answers (1)

RobP
RobP

Reputation: 9522

Well, it's ugly, but it can be done. Would make a lot more sense to have a table of questions and a table with a single row for each response to each a question, but if you can't do that you can do something like this, extending the SUM to include all 10 columns:

SELECT SUM(IF(essc_a1 LIKE '%es%', 1, 0) + IF(essc_a2 LIKE '%es%', 1, 0))
FROM employee_satisfaction_survey_checklist

Upvotes: 1

Related Questions