[英]Database SQL excluding a data
I have a problem getting data when joining 2 tables, JOB
and Application
.加入 2 个表
JOB
和Application
时,我在获取数据时遇到问题。
The Job
table has id, title and description, while the Application
table has id, applicant and job_id. Job
表有 id、title 和 description,而Application
表有 id、appliance 和 job_id。
I wanted to show the jobs that the applicant haven't applied yet我想显示申请人尚未申请的工作
ID TITLE DESCRIPTION
--------------------------------------
2 web developer some text
3 web designer some text
4 project manager some text
ID applicant job_id
-----------------------
1 28 2
2 1 2
3 1 3
So I tried this query:所以我尝试了这个查询:
select
job.id, job.title ,job.description
from
`job`
left join
`application` on `job`.`id` = `application`.`job`
where
`application`.`applicant` != 28
or `application`.`applicant` is null
but this just returns all jobs.但这只会返回所有工作。
You can use NOT IN
.您可以使用
NOT IN
。 So, the code should be look like given blow =>所以,代码应该看起来像给定的打击 =>
SELECT job.id, job.title ,job.description FROM JOB
WHERE ID NOT IN (SELECT job_id FROM Application);
You can use NOT EXISTS
as follows:您可以使用
NOT EXISTS
,如下所示:
select j.*
from `job` j
where not exists
(select 1 from `application` a
where j.`id` = a.`job` and a.`applicant` = 28)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.