Solved

Get counts from mysql database

Posted on 2011-03-20
5
241 Views
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.
0
Comment
Question by:nainil
5 Comments
 
LVL 9

Expert Comment

by:AriMc
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:

http://www.dynamicdrive.com/forums/showthread.php?t=41946

0
 
LVL 9

Expert Comment

by:fcontrepois
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)

<?php

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

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

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

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

mysql_free_result($result);

?>

0
 
LVL 39

Accepted Solution

by:
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
0
 

Author Comment

by:nainil
ID: 35176474
Thank you aarontomosky. It worked like a charm. Nice to learn something new :)
0
 
LVL 9

Expert Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Installer 5 37
Read a remote csv file from a https URL 8 47
PHP Soap Server returning XML but < & > as &lt; / &gt; 9 45
PHP Curl to output a url 7 47
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

839 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