Hi,
I have a mysql 'cities' table with data as follows:
id name ascii_name
1 Abilene, Texas Abilene-Texas
2 Akron, Ohio Akron-Ohio
3 Albuquerque, New-Mexico Albuquerque-New-Mexico
4 Alexandria, Virginia Alexandria-Virginia
5 Allentown, Pennsylvania Allentown-Pennsylvania
6 Amarillo, Texas Amarillo-Texas
etc...
I then have a 'Jobs' table that references the locations table.
I need to do a query that will return the number of jobs per state. How would I do this?
I'm not allowed to edit the database, only the function. The current function returns a list of cities and their job count, I need to edit this to return the states and their job count.
The current function looks like this:
public function GetJobsCountPerCity($excludeCitiesWithNoJobs)
{
global $db;
$jobsCountPerCity = array();
$sql = 'SELECT city_id, COUNT(id) AS total FROM jobs WHERE is_temp = 0 AND is_active = 1 GROUP BY city_id';
$result = $db->query($sql);
while ($row = $result->fetch_assoc())
$jobsCountPerCity[$row['city_id']] = $row['total'];
$cities = get_cities();
$result = array();
foreach ($cities as $city)
{
$count = 0;
// this check is needed because we don't have an entry if there are no jobs for a city
if (isset($jobsCountPerCity[$city['id']]))
$count = $jobsCountPerCity[$city['id']];
if ($count > 0)
$result[] = array('city_name' => $city['name'], 'jobs_in_city' => $count, 'city_ascii_name' => $city['ascii_name']);
else
{
if (!$excludeCitiesWithNoJobs)
$result[] = array('city_name' => $city['name'], 'jobs_in_city' => $count, 'city_ascii_name' => $city['ascii_name']);
}
}
return $result;
}
Thanks for your help!!