MySQL Forums
Forum List  »  PHP

percentage
Posted by: steven woodham
Date: March 30, 2006 04:12PM

I have a table that has the following columns:

Q1a, Q1b Q1c, Q2a, Q2b, Q2c, Q2d, Q2e etc etc

I want to create a query that returns each value entered for each of the questions, e.g. Q1a has possible values of Excellent, Good, Fair, Poor & N/A. I also want the query to return a percentage of the total records that represents each response if that makes sense and I have so far hit a brick wall on this.

As an example, the desired query would return the something like the following for each question:

Excellent 30%
Good 50%
Fair 10%
Poor 5%
N/A 5%

My query that I have started is as follows and until I get it right just deals with 1 question:



<?php
$query = "SELECT `value` FROM `valuesSurvey`";

$result = mysql_query($query) or die(mysql_error());

$query2 = "SELECT `1A`, COUNT(surveyID) FROM survey GROUP BY `1A`";

$result2 = mysql_query($query2) or die(mysql_error());

// Print out result
while(($row2 = mysql_fetch_array($result2)).($row = mysql_fetch_array($result))){
if($row2==NULL){
echo "There are no";
echo " ". $row['value'] ." answers.";
echo "<br />";
}else{

echo "There are ". $row2['COUNT(surveyID)'] ."";
echo " ". $row['value'] ." answers.";
echo "<br />";
}
}
?>
This returns data in the following format:

There are 24 Yes answers.
There are 3 No answers.
There are 7 Sometimes answers.
There are 9 N/A answers.

This gives me actual numbers but not the percentage of the total records.

Thanks in advance for your help.

Steven

Options: ReplyQuote


Subject
Written By
Posted
percentage
March 30, 2006 04:12PM
March 30, 2006 05:07PM
March 30, 2006 08:30PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.