PDA

View Full Version : Need help with a SELECT


mat
01-21-2004, 08:21 AM
I'm looping through job categories displaying the jobs currently available in that category:


// get all categories jobs from database
$sql_str = "SELECT v.id as job_id, j.id as cat_id, j.category_name as cat_name, j.category_description as cat_desc, v.title as title FROM vacancies v, job_category j WHERE j.id = v.job_category";


..
... build data structure, loop through and display down in the html
blah blah



I'm trying to get the page to display all categories even if there are no jobs currently in a category - it should look like this:
http://www.theory1.orcon.net.nz/right.gif (example 1)


Instead, if a job category doesn't currently have any jobs in it, then it's not displayed - like this
http://www.theory1.orcon.net.nz/right.gif (example 2)


I realise this is due to the WHERE in the SQL SELECT:
..WHERE j.id = v.job_category";

Is there a simple way around this? so that the category info displays anyway as in example 1

raf
01-21-2004, 09:39 AM
You need to use an outer join.

Like:
$sql_str_=_"SELECT vacancies.id as job_id, job_category.id as cat_id, job_category.category_name as cat_name, job_category.category_description as cat_desc, vacancies.title as title FROM job_category LEFT JOIN vacancies ON job_category.id = vacancies.job_category";

This wil include all records from job_category but only include the fields from vacancies if the two felds match. Else, the cells are empty (Null)