简体   繁体   English

简单的联接查询协助

[英]Simple Join Query Assistance

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: 这是documents表的转储:

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 tickets_jobs表将候选人连接到工作

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. tickets_jobs_documents表基于作业将文档连接到候选人。

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. 我想我需要在基于job.id和候选人.id的候选人表中使用INNER JOIN,但我无法弄清楚如何使用查询来处理它。 also, i dont know what to use for my WHERE clause. 另外,我不知道该为WHERE子句使用什么。

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? 我对候选人表文档的表有些困惑...也许您真的要在其中而不是候选人图标和工作ID在哪里呢?

Example of listing all documents created by Jonathan Kushner applying for Associate Sales Position. 列出所有由乔纳森·库什纳(Jonathan Kushner)创建的申请副销售职位的文档的示例。

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 这是SQL Fiddle上的工作代码预览: http ://sqlfiddle.com/#!9/9b16c/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM