Use one SQL statement to pull all records from a db within a certain date range.
From there I want to be able to count how many people answered "column1" a certain way.
So for example I have a table with "Employee Type". Is it possible for me to do this with just one SQL statement?
Faculty: [ # of people who answered "Faculty" in "Employee_Type" column ]
Staff: [ # of people who answered "Staff" in "Employee_Type" column ]
Thanks for any help I can get on this. It's been way too long since I've done any of this manual SQL/PHP stuff. I normally work in frameworks nowadays so my knowledge of the basics is... gone .
Your data will come in two rows(or more if you have more Employee_Types)
12 Staff
8 Faculty
Loop through your dataset and the row with the second column being Staff contains the number for how many Staff Employee_Types there are. Thats the row you want to display in your table.
If you make an attempt and post the code I can critique it for you.
Your data will come in two rows(or more if you have more Employee_Types)
12 Staff
8 Faculty
Loop through your dataset and the row with the second column being Staff contains the number for how many Staff Employee_Types there are. Thats the row you want to display in your table.
If you make an attempt and post the code I can critique it for you.
Ok, here's what I got so far:
Code:
$emp_type = "SELECT COUNT(*),`emp_type` FROM `responses` GROUP BY `emp_type`;";
$emp_type_results = mysql_fetch_assoc(mysql_query($emp_type));
echo '<pre>';
print_r($emp_type_results);
echo '</pre>';
Here's what it outputs:
Array
(
[COUNT(*)] => 3
[emp_type] => USS
)
Why does it only show the one record? There should be two different employee types.
In a manner of speaking, you need multiple queries. COUNT is an aggregate function and requires grouping. However, there are multiple ways to attack this. You could create the multiple queries and while loops or you can one big query with multiple queries in it. Subqueries are explained here - http://dev.mysql.com/doc//refman/5.0/en/subqueries.html.
// SQL Queries
$queries = array('emp_type' => 'SELECT COUNT(*) AS `total`, `emp_type` FROM `responses` GROUP BY `emp_type`;',
'length_of_service' => 'SELECT COUNT(*) FROM `responses` GROUP BY `length_of_service`;',
'discrimination' => 'SELECT COUNT(*), `q9_describe` FROM `responses` GROUP BY `q9`;');
// Create results array
$results = array();
// Employee Type
$emp_type_results = mysql_query($queries['emp_type']);
while ($row = mysql_fetch_assoc($emp_type_results)){
print_r($row);
if($row['emp_type'] == 'USS'){
$results['emp_type-uss'] = $row['total'];
}
if($row['emp_type'] == 'UPS'){
$results['emp_type-ups'] = $row['total'];
}
if($row['emp_type'] == null){
$results['emp_type-empty'] = $row['total'];
}
}
My problem:
If the 'emp_type' column is blank... I need it to count how many times it pulls blank 'emp_type's... I hope that makes sense.
I've attempted it up there... but doesn't seem to work at all. Doesn't display anything. I could set $results['emp_type-empty'] to 0 ahead of the while(), but I'd rather not have to do that.
Bookmarks