无法进行合适的查询
Unable to make a suitable query
我正在尝试创建一个数据库。它具有以下表格子集
- 职位(job_id,描述)
- 技能(skill_id,技能)
- jobs_and_skills (job_id, skill_id)
- applicant_skill (applicant_id , skills_id)
我的查询必须是:查找具有给定工作 ID 所需技能的求职者
我使用一系列子查询做了类似的事情
select DISTINCT applicant_id
from applicant_skill
where skill_id in
(select DISTINCT skill_id
from jobs_and_skill
where job_id in (select job_id
from jobs
where description ="research"));
但是在这里,如果我有一份需要技能的工作,比如 A、B、C,并且我有候选人只有技能 A,那么 return 那个候选人也会。有没有办法找到具备所有技能 A、B、C 的候选人,而不是 return 只具备所需技能子集的候选人
使用 JOIN
而不是 IN
。然后你可以加入一个子查询,计算一份工作所需的技能数量,并将其与申请人共有的技能数量进行比较。
SELECT as.applicant_id
FROM applicant_skill AS as
JOIN jobs_and_skills AS js ON js.skill_id = as.skill_id
JOIN jobs AS j ON j.job_id = js.job_id
JOIN (
SELECT job_id, COUNT(*) AS skill_count
GROUP BY job_id
) AS jsc ON j.job_id = jsc.job_id
WHERE j.description = "research"
GROUP BY as.applicant_id
HAVING COUNT(*) = jsc.skill_count
您可以在加入 applicants
之前为 jobs_and_skill 准备 subquery
。
select * from applicant_skill t1
inner join
(select t2.skill_id from
jobs_and_skill t2
inner join skills t3 on t3.skill_id = t2.skill_id
inner join jobs t4 on t4.job_id = t2.job_id
where t4.description = 'research') as t
on t1.skill_id = t.skill_id
您可以加入jobs
和applicants
,条件是确保申请人具备所有工作技能。
select
j.*,
a.*
from
jobs j
inner join applicants a on not exists (
select 1
from job_and_skills js
left join applicant_skills as
on as.applicant_id = a.applicant_id
and as.skill_id = js.skill_id
where
js.job_id = j.job_id
and as.applicant_id is null
)
where j.description = 'research'
连接条件使用 not exists
条件使用左连接反模式,其短语如下:申请人不拥有此职位所需的技能。
我正在尝试创建一个数据库。它具有以下表格子集
- 职位(job_id,描述)
- 技能(skill_id,技能)
- jobs_and_skills (job_id, skill_id)
- applicant_skill (applicant_id , skills_id)
我的查询必须是:查找具有给定工作 ID 所需技能的求职者
我使用一系列子查询做了类似的事情
select DISTINCT applicant_id
from applicant_skill
where skill_id in
(select DISTINCT skill_id
from jobs_and_skill
where job_id in (select job_id
from jobs
where description ="research"));
但是在这里,如果我有一份需要技能的工作,比如 A、B、C,并且我有候选人只有技能 A,那么 return 那个候选人也会。有没有办法找到具备所有技能 A、B、C 的候选人,而不是 return 只具备所需技能子集的候选人
使用 JOIN
而不是 IN
。然后你可以加入一个子查询,计算一份工作所需的技能数量,并将其与申请人共有的技能数量进行比较。
SELECT as.applicant_id
FROM applicant_skill AS as
JOIN jobs_and_skills AS js ON js.skill_id = as.skill_id
JOIN jobs AS j ON j.job_id = js.job_id
JOIN (
SELECT job_id, COUNT(*) AS skill_count
GROUP BY job_id
) AS jsc ON j.job_id = jsc.job_id
WHERE j.description = "research"
GROUP BY as.applicant_id
HAVING COUNT(*) = jsc.skill_count
您可以在加入 applicants
之前为 jobs_and_skill 准备 subquery
。
select * from applicant_skill t1
inner join
(select t2.skill_id from
jobs_and_skill t2
inner join skills t3 on t3.skill_id = t2.skill_id
inner join jobs t4 on t4.job_id = t2.job_id
where t4.description = 'research') as t
on t1.skill_id = t.skill_id
您可以加入jobs
和applicants
,条件是确保申请人具备所有工作技能。
select
j.*,
a.*
from
jobs j
inner join applicants a on not exists (
select 1
from job_and_skills js
left join applicant_skills as
on as.applicant_id = a.applicant_id
and as.skill_id = js.skill_id
where
js.job_id = j.job_id
and as.applicant_id is null
)
where j.description = 'research'
连接条件使用 not exists
条件使用左连接反模式,其短语如下:申请人不拥有此职位所需的技能。