Get counts from mysql database

Posted on 2011-03-20
Last Modified: 2012-05-11
My table has following structure (along with few other columns)
product      a1    a2     a3   a4
abc            yes      no      yes      no
pqr            yes      yes      no      yes
xyz            yes      yes      yes      yes

I need to run a query which will return the total count of yes:
product      count
abc              2
pqr               3
xyz              4

How can i return the same using PHP mySQL.
Question by:nainil

Expert Comment

ID: 35176216
I don't think that kind of query can be done with standard SQL but you could apply
the method and PHP-code described here:


Expert Comment

ID: 35176263
sql is done to work on rows not on columns, so I'll say that this work is more for the php part.

this code is written as is directly in this textarea without testing so I probably forgot a ; or something else but the logic is good (I hope)


mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());

$result = mysql_query("SELECT * FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
for ($i=1; $i<count($row); $i++){
  if($row[$i]=="yes"){ $yes++;}

printf("Product: %s  Yesses: %s", $product, $yes);  



LVL 38

Accepted Solution

Aaron Tomosky earned 500 total points
ID: 35176380
It can be done in SQL with a case statement and group by.
Here is the idea but I'm sure my syntax is a little off as I'm on my phone
Select product, sum(case when a1='yes'  then 1 else 0 end
+ case when a2 ='yes') as totalyes group by product

Author Comment

ID: 35176474
Thank you aarontomosky. It worked like a charm. Nice to learn something new :)

Expert Comment

ID: 35176788
really case made it to mysql , I'll sleep being less stupid tonight

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now