user782104
user782104

Reputation: 13545

How to query the count result in this scenario in MYSQL/SQL?

The table is like this

Result
------
type1_choice_1
type1_choice_2
type2_choice_1
type2_choice_2
type3_choice_1
type3_choice_2

There is 3 type, for each type, there is 2 choice, for each choice , the possible data is 1 to 10. For example, the type1_choice_1 can be 5, then type1_choice_2 can be 6

I have already insert the data. The problem is I need to query the result. for each column, count the number people choice

That means, e.g. for type1_choice_1, I need to know how many people choose 1, how many choose 2 .... to how many choose 10 . It is the same for other choice

How can I make the query that is most efficient (and readable as well)? Thanks for helping

*note : Highly appreciate if use the codeigniter syntax, but that does not matter, I can convert the MySQL query to it.

Upvotes: 0

Views: 135

Answers (1)

Ankit Bajpai
Ankit Bajpai

Reputation: 13509

You can try somthing like this:-

SELECT type, choice, COUNT(data)
FROM your_table
GROUP BY choice, type;

Upvotes: 1

Related Questions