简体   繁体   English

如何从表SQL获取不存在的记录?

[英]How to get not exist records from table SQL?

Sorry if my question was stupid or basic.I trying to get not exist id records from db. 抱歉,如果我的问题是愚蠢的还是基本的。我试图从db中获取不存在的id记录。 But it is showing all records. 但是它显示所有记录。

---------------------+
id  |  name  |  age  |
---------------------+
 1  |   ase  |   33  |
 3  |   ase  |   33  |
 4  |   ase  |   33  |
 5  |   ase  |   33  |
 7  |   ase  |   33  |
 9  |   ase  |   33  |
---------------------+

i tried something like this 我尝试过这样的事情

SELECT * FROM tablename WHERE id NO IN ('1','2','4','5','6','7') but it showing all records. SELECT * FROM表名WHERE ID NO IN('1','2','4','5','6','7')但显示所有记录。 In this given query id 2,6 are not exist in table show i need to show them as result. 在此给定的查询ID 2,6在表show中不存在,我需要将其显示为结果。 please some one help me to solve this. 请有人帮我解决这个问题。 thanks 谢谢

expecting output like 期望输出像

----------------------+
id not exist in table |
----------------------+
          2           |
          6           |
----------------------+

from the given IN statement this ID are not exist in TABLE something like this. 从给定的IN语句中,此ID在TABLE中不存在,如下所示。 I need to show id which are not exist in table as result. 我需要显示表中不存在的ID作为结果。

You need to use a (derived) table of values. 您需要使用(派生的)值表。 Here is a solution using left join instead of not in : 这是使用left join而不是not in使用的解决方案:

select n.n
from (select 1 as n union all select 2 union all select 4 union all select 5 union all
      select 6 union all select 7
     ) n left join
     tablename t
     on t.id = n.n
where t.id is null;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM