问题描述
我认为我正朝着正确的方向前进...请忍受,因为我的SQL并不是最好的
I think I'm going down the right path with this one... Please bear with me as my SQL isn't the greatest
我正在尝试查询数据库,以从一个表中选择所有单元格中不存在的所有内容.没什么意义,但是我希望这段代码会
I'm trying to query a database to select everything from one table where certain cells don't exist in another. That much doesn't make a lot of sense but I'm hoping this piece of code will
SELECT * from employees WHERE NOT EXISTS (SELECT name FROM eotm_dyn)
所以基本上,我有一张桌子,上面列出了员工及其详细信息.然后是另一个表,其中包含一些其他详细信息,包括它们的名称.在eotm_dyn表中没有名称的地方,这意味着它们没有条目,我想确切地知道他们是谁,或者换句话说,确切地知道缺少什么.
So basically I have one table with a list of employees and their details. Then another table with some other details, including their name. Where there name is not in the eotm_dyn table, meaning there is no entry for them, I would like to see exactly who they are, or in other words, see what exactly is missing.
上面的查询什么也没有返回,但是我知道缺少20个名字,所以我显然做错了.
The above query returns nothing, but I know there are 20ish names missing so I've obviously not gotten it right.
任何人都可以帮忙吗?
推荐答案
您没有在查询中加入表.
You didn't join the table in your query.
您的原始查询将始终不返回任何内容,除非eotm_dyn
中根本没有记录,在这种情况下,它将返回所有内容.
Your original query will always return nothing unless there are no records at all in eotm_dyn
, in which case it will return everything.
假定这些表应在employeeID
上联接,请使用以下命令:
Assuming these tables should be joined on employeeID
, use the following:
SELECT *
FROM employees e
WHERE NOT EXISTS
(
SELECT null
FROM eotm_dyn d
WHERE d.employeeID = e.id
)
您可以使用LEFT JOIN
关键字将这些表联接起来,并过滤掉NULL
的关键字,但这可能会比使用NOT EXISTS
的效率低.
You can join these tables with a LEFT JOIN
keyword and filter out the NULL
's, but this will likely be less efficient than using NOT EXISTS
.
这篇关于选择*不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!