SELECT * WHERE NOT EXISTS(在不存在的地方选择 *)
问题描述
我认为我正在走这条正确的道路......请容忍我,因为我的 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 表中没有 name 的地方,这意味着他们没有条目,我想看看他们到底是谁,或者换句话说,看看到底缺少什么.
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.
这篇关于在不存在的地方选择 *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在不存在的地方选择 *
基础教程推荐
- 带更新的 sqlite CTE 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
