简体   繁体   English

SQL语句 - 排除某些数据

[英]SQL Statement - Excluding certain data

I'm still in the learning phase of SQL statements and I'm hoping someone out there can help. 我仍处于SQL语句的学习阶段,我希望有人可以提供帮助。

I have a many-to-many database base relationship. 我有一个多对多的数据库基础关系。

The table Department can have multiple Jobs associated with it and and Jobs can be related to multiple Departments. 表部门可以有多个与之关联的作业,而作业可以与多个部门相关联。 So I have this basic relationship type. 所以我有这种基本的关系类型。

Job.ID (one-to-many) Jobs.JobID
Jobs.DepartmentID (many-to-one) Department.ID

What I'm trying to do is get a list of Jobs that aren't already associated with a department. 我要做的是获取一个尚未与部门关联的工作列表。

tbl=Job
ID  Job     Active
1   10-3242  Yes
2   12-3902  Yes
3   12-3898  Yes

tbl=Jobs
ID  DepartmentID    JobID
1        3            1
2        3            2

tbl=Department
ID  Department
1   Administration
2   Sales
3   Production

Query: 查询:

string sql = "SELECT Job FROM (Job " +
    "INNER JOIN Jobs ON Job.ID = Jobs.JobID) " +
    "INNER JOIN Department ON Jobs.DepartmentID = Department.ID " +
    "WHERE Department.Department <> 'Production'";

I'm expecting the job code 12-3898 to be returned but obviously I'm forgetting something. 我期待返回工作代码12-3898 ,但显然我忘记了什么。

Any assistance would be great. 任何援助都会很棒。 Cheers. 干杯。

You can use a LEFT JOIN . 您可以使用LEFT JOIN The LEFT JOIN keyword returns all rows from the left table with the matching rows in the right table. LEFT JOIN关键字返回左表中的所有行以及右表中的匹配行。 The result is NULL in the right side if there is no match. 如果没有匹配,则结果在右侧为NULL Since you want the jobs without a matching department, you can check if the joined DepartmentID is NULL : 由于您希望没有匹配部门的作业,您可以检查加入的DepartmentID是否为NULL

SELECT Job.Job
FROM   Job LEFT JOIN Jobs ON Job.ID = Jobs.JobID
WHERE  Jobs.DepartmentID IS NULL;

Checkout this demo . 看看这个演示 Let me know if it works. 如果有效,请告诉我。

select  job
from    job
where   id not in   (select jobId
                    from    jobs)

Unfortunately, I'm not in an environment where I can test the results. 不幸的是,我不在一个可以测试结果的环境中。 However the basic thought process behind this is whenever you want to return rows from a table that do NOT have a matching row from another table, you have to do an outer join. 但是,这背后的基本思考过程是,只要您想要从没有来自另一个表的匹配行的表中返回行,就必须执行外部联接。 The intention being you want to display ALL the rows from the job table (ie left outer join). 目的是要显示作业表中的所有行(即左外连接)。 However you want to filter where the departmentID IS NULL, because the NULL departmentID will be the row in the job table with no matching departmentID from the department table. 但是,您希望过滤departmentID为NULL的位置,因为NULL departmentID将是作业表中的行,而department表中没有匹配的departmentID。 Hope that helps. 希望有所帮助。

SELECT j.id, j.job, j.active, jd.departmentid
FROM job j
LEFT OUTER JOIN jobs jd ON j.id = jd.jobid
LEFT OUTER JOIN department d ON d.id = jd.departmentid
WHERE jd.departmentid IS NULL

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

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