I have the following SQL query and it’s not returning the desired results. I think it has something to do with the where clause (in particular the l.postcode field).
Here is some data that I have inside the jobs_locations table:
Given the data the SQL query should return the second row but it doesn’t. Any ideas?
SELECT jobs.job_title, jobs.job_url, jobs.job_salary, jobs.job_ote, jobs.job_perks, jobs.job_description, jobs.date_posted, companies.company_name, companies.company_url, companies.logo_thumbnail, jobs_industries.industry_id, jobs_job_types.job_type_id, count( applications.id ) AS applications, count( featured_jobs.id ) AS featured, l.display_name, l.postcode
FROM jobs
INNER JOIN companies ON companies.id = jobs.company_id
LEFT OUTER JOIN applications ON applications.job_id = jobs.id
LEFT OUTER JOIN featured_jobs ON featured_jobs.job_id = jobs.id
INNER JOIN jobs_industries ON jobs_industries.job_id = jobs.id
INNER JOIN jobs_job_types ON jobs_job_types.job_id = jobs.id
INNER JOIN (
SELECT job_id, if( count( job_id ) >1, 'Locations throughout the UK', display_name ) AS display_name, postcode
FROM jobs_locations
GROUP BY job_id
) AS l ON l.job_id = jobs.id
WHERE jobs.status = 'on'
AND l.postcode = 'IG7'
GROUP BY jobs.id
ORDER BY featured DESC , date_posted DESC
I always put all INNER JOINs first, and LEFT JOINs last, so I would write that query like this:
SELECT jobs.job_title, jobs.job_url, jobs.job_salary, jobs.job_ote, jobs.job_perks, jobs.job_description, jobs.date_posted, companies.company_name, companies.company_url, companies.logo_thumbnail, jobs_industries.industry_id, jobs_job_types.job_type_id, count( applications.id ) AS applications, count( featured_jobs.id ) AS featured, l.display_name, l.postcode
FROM jobs
INNER JOIN companies ON companies.id = jobs.company_id
INNER JOIN jobs_industries ON jobs_industries.job_id = jobs.id
INNER JOIN jobs_job_types ON jobs_job_types.job_id = jobs.id
INNER JOIN (
SELECT job_id, if( count( job_id ) >1, 'Locations throughout the UK', display_name ) AS display_name, postcode
FROM jobs_locations
GROUP BY job_id
) AS l ON l.job_id = jobs.id
LEFT OUTER JOIN applications ON applications.job_id = jobs.id
LEFT OUTER JOIN featured_jobs ON featured_jobs.job_id = jobs.id
WHERE jobs.status = 'on'
AND l.postcode = 'IG7'
GROUP BY jobs.id
ORDER BY featured DESC , date_posted DESC
Thanks for the post, unfortunately it didn’t work though.
I think it’s because I have multiple rows in the jobs_locations table. When the postcode is CM14 for example it works (maybe because it’s the first row). All other postcodes do not work.
Are you sure you have related data in all joined tables?
In phpMyAdmin, you could start with a query on the jobs table, and then add the joins one by one to see which one is causing the postcode ‘IG7’ not to return any rows.
I managed to strip it down to this, but still getting no rows…
SELECT jobs.job_title, jobs.job_url, jobs.job_salary, jobs.job_ote, jobs.job_perks, jobs.job_description, jobs.date_posted, l.display_name, l.postcode
FROM jobs
INNER JOIN (
SELECT job_id, if( count( job_id ) >1, 'Locations throughout the UK', display_name ) AS display_name, postcode
FROM jobs_locations
GROUP BY job_id
) AS l ON l.job_id = jobs.id
WHERE l.postcode = 'IG7'
GROUP BY jobs.id
ORDER BY date_posted DESC
LIMIT 0 , 30
I changed a few things around and now I have the new query below. It works well. The only thing is it doesn’t give me a list of all display_name rows matching the job id - it always just returns the 1 (for example Brentwood, Essex). This means that my if statement never gets executed.
if( count( job_id ) >1, 'Locations throughout the UK', display_name ) AS display_name
How could I achieve this?
SELECT
jobs.job_title,
jobs.job_url,
jobs.job_salary,
jobs.job_ote,
jobs.job_perks,
jobs.job_description,
jobs.date_posted,
companies.company_name,
companies.company_url,
companies.logo_thumbnail,
count( applications.id ) AS applications,
count( featured_jobs.id ) AS featured,
l.display_name,
l.postcode,
l.display_county
FROM jobs
INNER JOIN companies ON companies.id = jobs.company_id
INNER JOIN jobs_industries ON jobs_industries.job_id = jobs.id
INNER JOIN jobs_job_types ON jobs_job_types.job_id = jobs.id
INNER JOIN (
SELECT job_id, if( count( job_id ) >1, 'Locations throughout the UK', display_name ) AS display_name, postcode, display_county
FROM jobs_locations
GROUP BY postcode
) AS l ON l.job_id = jobs.id
LEFT OUTER JOIN applications ON applications.job_id = jobs.id
LEFT OUTER JOIN featured_jobs ON featured_jobs.job_id = jobs.id
WHERE jobs.status = 'on'
AND l.postcode = 'CM14'
GROUP BY jobs.id
ORDER BY featured DESC , date_posted DESC
SELECT job_id, if( count( job_id ) >1, 'Locations throughout the UK', display_name ) AS display_name, postcode, display_county
FROM jobs_locations
GROUP BY postcode
this query produces one row per postcode
which job_id do you think it will return for each postcode?