SQL - NOT IN 解释

发布于 2024-08-20 11:55:54 字数 714 浏览 2 评论 0原文

我正在从事一个需要在 SQL 结果中获得最高性能的项目,并且正在寻求优化查询,但经过一些尝试和错误后,我在 IN 方面遇到了一些问题。

-- THIS RETURNS NO RESULTS AT ALL.
SELECT sysdba.HISTORY.TICKETID FROM sysdba.HISTORY  
     where TICKETID = 't6UJ9A002MJC'                                         

-- THIS RETURNS 4 RESULTS.
SELECT sysdba.C_TICKETPROVIDERS.* FROM sysdba.C_TICKETPROVIDERS 
     where sysdba.C_TICKETPROVIDERS.TICKETID = 't6UJ9A002MJC'

-- THIS RETURNS NO RESULTS AT ALL.
SELECT sysdba.C_TICKETPROVIDERS.* FROM sysdba.C_TICKETPROVIDERS 
     where sysdba.C_TICKETPROVIDERS.TICKETID NOT IN 
        (SELECT DISTINCT sysdba.HISTORY.TICKETID FROM sysdba.HISTORY) 

由于没有匹配结果,最后一个查询结果不应该至少与前一个查询结果有4个相同的结果吗???

I am working in a project which needs top performance in SQL results, and was looking to optimize a query, but after some trial and error I am having some trouble with IN.

-- THIS RETURNS NO RESULTS AT ALL.
SELECT sysdba.HISTORY.TICKETID FROM sysdba.HISTORY  
     where TICKETID = 't6UJ9A002MJC'                                         

-- THIS RETURNS 4 RESULTS.
SELECT sysdba.C_TICKETPROVIDERS.* FROM sysdba.C_TICKETPROVIDERS 
     where sysdba.C_TICKETPROVIDERS.TICKETID = 't6UJ9A002MJC'

-- THIS RETURNS NO RESULTS AT ALL.
SELECT sysdba.C_TICKETPROVIDERS.* FROM sysdba.C_TICKETPROVIDERS 
     where sysdba.C_TICKETPROVIDERS.TICKETID NOT IN 
        (SELECT DISTINCT sysdba.HISTORY.TICKETID FROM sysdba.HISTORY) 

Shouldn't the last query result at least the same 4 results as the previous one, since there are no matching results???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我们只是彼此的过ke 2024-08-27 11:55:55

“SELECT DISTINCT sysdba.HISTORY.TICKETID FROM sysdba.HISTORY”返回 null,因此最后一个查询不返回任何行。无法与这样的空结果进行比较。使用“不存在”。

"SELECT DISTINCT sysdba.HISTORY.TICKETID FROM sysdba.HISTORY" returns null, hence the last query doesnt return any rows. Cant compare against null result like that. Use "NOT EXISTS".

难理解 2024-08-27 11:55:55

WHERE 子句中的 NOT IN (NULL) 部分返回 false。

尝试使用:

SELECT sysdba.C_TICKETPROVIDERS.* FROM sysdba.C_TICKETPROVIDERS 
     where NOT EXISTS 
         (SELECT TOP 1 1 
          FROM sysdba.HISTORY
          WHERE sysdba.HISTORY.TICKETID  = sysdba.C_TICKETPROVIDERS.TICKETID) 
  • 子查询至少查找一条记录。
    • 如果找到,则返回 1,并且 NOT EXISTS 子句失败,正如预期的那样。
    • 如果没有找到,则返回NULL,并且NOT EXISTS将成功。

The piece NOT IN (NULL) in your WHERE clausole returns false.

Try with:

SELECT sysdba.C_TICKETPROVIDERS.* FROM sysdba.C_TICKETPROVIDERS 
     where NOT EXISTS 
         (SELECT TOP 1 1 
          FROM sysdba.HISTORY
          WHERE sysdba.HISTORY.TICKETID  = sysdba.C_TICKETPROVIDERS.TICKETID) 
  • The subquery seeks for at least one record.
    • If it finds it, returns 1, and the NOT EXISTS clausole fails, as expected.
    • If it doesn't find it, returns NULL, and the NOT EXISTS will succeed.
江湖彼岸 2024-08-27 11:55:55

只是为了好玩,试试这个:

SELECT sysdba.C_TICKETPROVIDERS.* FROM sysdba.C_TICKETPROVIDERS  
     where sysdba.C_TICKETPROVIDERS.TICKETID NOT IN  
        (SELECT DISTINCT sysdba.HISTORY.TICKETID 
         FROM sysdba.HISTORY 
         Union
         Select 'xxxxxxxxxxxxxxxx') 

如果这有效,你的问题是在你的平台上,Not In将无法针对空集正常运行...

Just as a lark, try this:

SELECT sysdba.C_TICKETPROVIDERS.* FROM sysdba.C_TICKETPROVIDERS  
     where sysdba.C_TICKETPROVIDERS.TICKETID NOT IN  
        (SELECT DISTINCT sysdba.HISTORY.TICKETID 
         FROM sysdba.HISTORY 
         Union
         Select 'xxxxxxxxxxxxxxxx') 

If this works, yr problem is that on your platform, Not In will not function properly against an EMpty set...

青丝拂面 2024-08-27 11:55:55

查询(SELECT DISTINCT sysdba.HISTORY.TICKETID FROM sysdba.HISTORY) 返回NULL

您有:

勾选 id 1 is NOT IN (NULL)? FALSE

勾选 id 2 不在 (NULL)? FALSE

勾选 id 3 不在(NULL)? FALSE

....等等...

没有记录可以匹配您的where条件NOT IN。所以你根本得不到任何结果。

尝试使用以下查询:

SELECT sysdba.C_TICKETPROVIDERS.* FROM sysdba.C_TICKETPROVIDERS 
     where NOT EXIST  
        (SELECT TOP 1 sysdba.HISTORY.TICKETID FROM sysdba.HISTORY
         WHERE  HISTORY.TICKETID=sysdba.C_TICKETPROVIDERS.TICKETID)

The query (SELECT DISTINCT sysdba.HISTORY.TICKETID FROM sysdba.HISTORY) return NULL.

You have:

ticked id 1 is NOT IN (NULL)? FALSE

ticked id 2 is NOT IN (NULL)? FALSE

ticked id 3 is NOT IN (NULL)? FALSE

....and so on....

No record can match your where condition NOT IN. So you get NO RESULTS AT ALL.

Try with this query:

SELECT sysdba.C_TICKETPROVIDERS.* FROM sysdba.C_TICKETPROVIDERS 
     where NOT EXIST  
        (SELECT TOP 1 sysdba.HISTORY.TICKETID FROM sysdba.HISTORY
         WHERE  HISTORY.TICKETID=sysdba.C_TICKETPROVIDERS.TICKETID)
芸娘子的小脾气 2024-08-27 11:55:55

sysdba.History.TicketID 和 sysdba.C_TICKETPROVIDERS.TICKETID 在数据类型和长度方面是否匹配?

我还发现 group by 比 unique 更快。

最后检查数据库的排序规则类型,排序规则可能区分大小写,并且由于某种原因一个是大写的,另一个不是大写的?

您能提供表架构吗?

Do the sysdba.History.TicketID and sysdba.C_TICKETPROVIDERS.TICKETID match in terms of datatype and length?

I've also found group by's to be quicker than distinct.

Lastly check the collation types for the database, it might be that the collation is case sensitive and for some reason one is uppercased and the other isnt?

Can you provide the table schemas?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文