对于我试图为Oracle SQL数据库编写的查询,我只是试图获取CI表中的所有行,这些行由sys_user表中未列出的某人/某些东西所拥有,但结果返回了实际上在sys_user表中找到了手动检查下面的查询。 Stranger stil,owner_id的所有返回值都是相同的。
SELECT
ci.sys_id as product_id,
ci.name as product,
ci.sys_class_name as class_name,
ci.owned_by as owner_id
FROM
EDQ_EDW.CMDB_CI CI,
EDQ_EDW.CMDB_SYS_USER usr
WHERE
ci.owned_by is not null
and ci.owned_by != all(usr.sys_id)
我已经使用Oracle文档进行了双重检查,以确保我没有使用!= ALL(),并且我不相信自己。造成这种情况的原因是什么?
答案 0 :(得分:1)
ALL
,SOME
和ANY
条件来自行集,而不是像查询中的标量值。尝试这样的事情:
SELECT
ci.sys_id as product_id,
ci.name as product,
ci.sys_class_name as class_name,
ci.owned_by as owner_id
FROM EDQ_EDW.CMDB_CI CI
WHERE ci.owned_by is not null
AND ci.owned_by != ALL (SELECT sys_id FROM EDQ_EDW.CMDB_SYS_USER)
我不确定逻辑是否正确,尽管语法应该没问题。如果您没有获得所需的结果,请尝试使用NOT IN
:
SELECT
ci.sys_id as product_id,
ci.name as product,
ci.sys_class_name as class_name,
ci.owned_by as owner_id
FROM EDQ_EDW.CMDB_CI CI
WHERE ci.owned_by is not null
AND ci.owned_by NOT IN (SELECT sys_id FROM EDQ_EDW.CMDB_SYS_USER)
你也可以使用NOT EXISTS
或"左连接并检查NULL"查询。在某些情况下,这些更快。您可以使用漂亮,简单的示例here找到解释。