简体   繁体   English

SQL查询:列出一个表中未出现在另一个表中的所有项

[英]SQL query: list all items in one table that do not appear in another table

I'm working on a training tracker program and I'm at a point where I can't figure out the SQL query. 我正在研究一个培训跟踪器程序,我正处于无法弄清SQL查询的地步。

I have 3 tables: employees, trainingRecords, masterList . 我有3个表: employees, trainingRecords, masterList

employees and trainingRecords are related through the empID fkey. employeestrainingRecords通过empID fkey相关。

trainingRecords and masterList are related through the TID fkey. trainingRecordsmasterList通过TID fkey相关联。

Right now the training records table is blank because nothing has been entered (all employees have no training). 现在,培训记录表是空白的,因为没有输入任何内容(所有员工都没有接受过培训)。

I want to populate a listbox with all of the items in the masterList that are unaccounted for in the trainingRecords table. 我想填充一个列表框,其中包含masterList中所有在trainingRecords表中未列出的项目。

Since the trainingRecords table is blank, it should be returning lName, fName from the employees table and docName, docNumber for all entries in the master list. 由于trainingRecords表是空白的,它应该返回lName, fNameemployees表和docName, docNumber在主列表中的所有条目。

I'm stumped. 我很难过。 Any suggestions? 有什么建议?

I'm assuming you want to display all employees multiple times with the training documents they have not done yet. 我假设你想用他们尚未完成的培训文件多次显示所有员工。

SELECT a.lName, a.fName, b.docNumber, b.docName 
FROM
(SELECT e.lName, e.fName, t.TID 
 FROM employees e
 LEFT JOIN trainingRecords t ON e.empID = t.empID
) AS a,
(SELECT m.docNumber, m.docName, t.TID
 FROM masterList m
 LEFT JOIN trainingRecords t ON m.TID = t.TID
) AS b
WHERE a.TID IS NULL OR b.TID IS NULL
ORDER BY a.lName, b.docNumber

example results: 示例结果:

lName     fName  docNumber          docName
Simpson   Homer     1      Nuclear Physics for Dummies
Simpson   Homer     2      Nuclear Physics for Beginners
Simpson   Homer     3      Advanced Nuclear Physics
Simpson   Lisa      3      Advanced Nuclear Physics

您想要LEFT JOIN,在连接的左侧将是您知道将包含所有内容的表,而右侧将是您正在测试的内容。

select masterList.* from masterList LEFT JOIN trainingRecords ON(masterList.TID = trainingRecords.TID) WHERE trainingRecords.TID IS NULL; 

Okay, you have to JOIN all three tables with the trainingRecords table in the middle because it has the columns necessary to link the other two tables. 好的,您必须在中间使用trainingRecords表加入所有三个表,因为它具有链接其他两个表所需的列。 Your query will look something like this: 您的查询将如下所示:

 SELECT E.lName, E.fName, ML.docName, ML.docNumber FROM
   (employees E LEFT OUTER JOIN trainingRecords TR ON E.empID = TR.empID)
                RIGHT OUTER JOIN masterList ML ON ML.TID = TR.TID
   WHERE TR.TID IS NULL

What's happening here? 这里发生了什么事?

First, you're doing a LEFT OUTER JOIN of employees and trainingRecords. 首先,您正在进行员工和培训记录的LEFT OUTER JOIN。 The LEFT OUTER is to ensure that all the records from employees show up even if there's no match in trainingRecords (which of course don't exist since trainingRecords has no data at all). LEFT OUTER是为了确保员工的所有记录都显示,即使在trainingRecords中没有匹配(由于trainingRecords完全没有数据,因此当然不存在)。

Then, you're taking the results of that query and RIGHT OUTER JOINing them to masterList. 然后,您将获取该查询的结果并将其直接加入到masterList中。 The RIGHT OUTER guarantees that all masterList records will be included even if there are no matches in trainingRecords. RIGHT OUTER保证即使trainingRecords中没有匹配项,也会包含所有masterList记录。

Finally, WHERE TR.TID IS NULL filters out any records that actually matched any (future) records in trainingRecords. 最后,WHERE TR.TID IS NULL过滤掉实际匹配trainingRecords中任何(未来)记录的任何记录。

Why not use Full Join? 为什么不使用Full Join? What I use is: 我用的是:

Select A.* from A Full Join B on A.ID = B.ID where B.ID is NULL

暂无
暂无

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

相关问题 SQL 查询 - 一个表中而不是另一个表中的项目列表 - SQL query - list of items in one table not in another 选择表中未出现在另一个表的外键中的所有项 - Select all items in a table that do not appear in a foreign key of another table 列出一个表中不在另一个表中的所有项目 - list all items from one table that are not in another MS ACCESS 查询联结表,用于一个表中的所有项目,但不在另一个表中 - MS ACCESS Query with junction table, for all items in one table, but not in another 如何从一个SQL表中选择未出现在另一表中的项目 - How can I select items from one sql table that don't appear in another table MySQL查询以获取不属于另一个表的所有项目 - MySQL query to get all items that do not belong to another table SQL查询,如何从一个列表中获取所有元素,而从另一表中仅获取相似元素 - SQL Query, how to get all elements from one list and only the similar ones from another table SQL查询-从一个表中选择全部,在另一个表中匹配记录 - SQL Query - select all from one table with matching records in another 如何从一个表中选择项目,以使另一表中的查询恰好产生一行? - How do I select items from one table such that a query in another table yields exactly one row? 编写SQL查询以列出cd表中所有cd上出现的所有记录的记录id和记录日期 - Write a SQL query to list the recording id and recording date of all recordings that appear on all of the cds in the cd table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM