I'd like some assistance in fixing this query. This problem is fairly simple but I'm just not able to solve it. Let me explain the background on what it currently does, and then what I need to integrate.
First, here are my current tables.
candidates table
the candidates table lists all job candidates.
candidates
-----------
id
hash
first_name
last_name
here is a dump of the candidates table:
id hash first_name last_name
1 092ee63c6698851ba72183388bff1b3b Jonathan Kushner
2 2d234affe1d08c3394cd499f297d7380 Dustin Kushner
documents table
the documents table lists all documents in the system
documents
----------
id
hash
title
source_file
here is a dump of the documents table:
id hash title
1 436etgfgdggdd Associate Sales Resume
2 54645655gggdgdg Senior Officer Cover Letter
3 ffsafsdsfdfdssf Customer Service Resume
jobs table
the jobs table lists all jobs in the system
jobs
----------
id
hash
title
here's a dump of the jobs table:
id hash title
1 asfsfafafafdf Associate Sales Position
2 fddfd7sd7sd7sd7 Senior Officer Position
3 f9df89df8sd89sd Customer Service Position
candidates_jobs table
the candidates_jobs table connects candidates to a job
candidates_jobs
----------
id
hash
candidate_id
job_id
here's a dump of the candidates_jobs table:
id hash job_id candidate_id
1 89efaed413163be4557133266ce295b4 1 1
2 afsdfdssdfsdfsdfsdfsdfsdfsdfsdfd 1 2
3 df08dfs08sd80sdf80sdf80sd8sd8sd8 1 3
candidates_jobs_documents
the candidates_jobs_documents table connects documents to a candidate based on a job.
candidates_jobs_documents
----------
id hash job_id candidate_id document_id
1 saasdfasdasdasdasdasdasdasdasdas 1 1 1
2 dfasasasasasasasasdddsdasdasdasd 1 2 2
3 sdfsdfsdsdsdfadfsssssssddssdsdfd 1 3 3
OK. Now let me show you my current query:
SELECT
`users`.`first_name`,
`users`.`last_name`,
`documents`.`title`
FROM
`documents`
LEFT JOIN `candidates_jobs_documents`
ON `candidates_jobs_documents`.`document_id` = `documents`.`id`
LEFT JOIN `jobs`
ON `jobs`.`id` = `candidates_jobs_documents`.`job_id`
LEFT JOIN `candidates`
ON candidates.id = candidates_jobs_documents.candidate_id
LEFT JOIN `candidates_jobs`
ON `candidates_jobs`.`candidates_id` = `candidates`.`id`
ORDER BY `candidates`.`last_name` ASC
I think I need to use an INNER JOIN on the candidates_jobs_documents table based on job.id and candidate.id but I cant figure out how to handle it with the query. also, i dont know what to use for my WHERE clause.
The query isn't right. I need to grab the documents for a specific candidate based on the job. As you are aware, the documents are pinned to a candidates job, enabling the candidates to upload documents for any job and having that document as the document for that specific job.
Any help would be appreciated.
SELECT candidates.first_name, candidates.last_name, documents.title
FROM candidates, documents, candidates_jobs_documents
WHERE candidates.id = candidates_jobs_id.candidate_id AND
documents.id = candidates_jobs_documents.document_id
ORDER BY candidates.last_name ASC;
Not sure if this is what you need, but maybe. I'm a little confused by candidates_jobs_documents table... maybe you really want candidates_jobs.id in there instead of candidate.id and job_id?
Example of listing all documents created by Jonathan Kushner applying for Associate Sales Position.
SELECT
`users`.`first_name`,
`users`.`last_name`,
`docs`.`title`
FROM
`candidates` as `users`
INNER JOIN (
`candidates_jobs_documents` AS cjd
INNER JOIN `jobs` AS j ON (cjd.`job_id` = j.id)
INNER JOIN `documents` AS `docs` ON (cjd.`document_id` = docs.id)
) ON (`users`.id = cjd.`candidate_id`)
WHERE
j.title = 'Associate Sales Position'
AND users.first_name = 'Jonathan'
AND users.last_name = 'Kushner'
Here is working code preview at SQL Fiddle: http://sqlfiddle.com/#!9/9b16c/1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.