使用NOT IN可以从这个查询中得到什么?

时间:2013-12-19 18:30:47

标签: mysql

如果您尝试运行以下查询,结果是什么?假设对于此查询,所有书籍都具有publ_id的值,并且没有publ_id为90

Select *  
From a_bkinfo.books 
Where publ_id not in (90, null);

我运行了查询但没有收到任何行,但我认为这会返回publ_id不是90的行。我很困惑。我没看到什么?

Table:
-- create publishers
create table a_bkinfo.publishers (
   publ_id           integer          not null
 , publ_name         varchar(25)      not null  
 , constraint bk_publishers_pk        primary key(publ_id) 
 , constraint publ_id_range check (publ_id >1000)
)engine = INNODB;

Inserts:
-- publishers
Insert into a_bkinfo.publishers values (9000, 'Microsoft Press') ;
Insert into a_bkinfo.publishers values (9456, 'New Directions') ;
Insert into a_bkinfo.publishers values (9102, 'Alfred A. Knopf') ;
Insert into a_bkinfo.publishers values (9325, 'Addison Wesley') ;
Insert into a_bkinfo.publishers values (9745, 'Morgan Kaufmann') ;
Insert into a_bkinfo.publishers values (9521, 'Benjamin/Cummings') ;
Insert into a_bkinfo.publishers values (9822, 'O''Reilly') ; 
Insert into a_bkinfo.publishers values (9030, 'McGraw Hill') ;
Insert into a_bkinfo.publishers values (9444, 'APress') ;
Insert into a_bkinfo.publishers values (9528, 'Manning');
Insert into a_bkinfo.publishers values (9020, 'Princeton Univer Press') ;
Insert into a_bkinfo.publishers values (9021, 'Yale University Press') ;
Insert into a_bkinfo.publishers values (9022, 'Havard University Press') ;
Insert into a_bkinfo.publishers values (9507, 'J.Q. Vanderbildt');
Insert into a_bkinfo.publishers values (9664, 'WROX') ;
Insert into a_bkinfo.publishers values (9825, 'MySQL Press') ;
Insert into a_bkinfo.publishers values (9623, 'Prentice Hall') ;
Insert into a_bkinfo.publishers values (9725, 'Springer') ;
Insert into a_bkinfo.publishers values (9561, 'Houghton Mifflin'); 
Insert into a_bkinfo.publishers values (9902, 'W.W. Norton ') ;
Insert into a_bkinfo.publishers values (9023, 'Holt Paperbacks') ;
Insert into a_bkinfo.publishers values (9024, 'Univ of California Press') ;
Insert into a_bkinfo.publishers values (9776, 'Simon and Schuster') ;

2 个答案:

答案 0 :(得分:1)

它将返回一个空集。

90不在您的值部分....

  

选择*来自发布商   publ_id不喜欢'90%';

这将找到所有以90开头且之后有数字组合的那些。

通过添加模数符号并抛出单引号,您需要查找

的字符串表示形式

9 0_ _

<强>更新

对不起,我的初始条款是在寻找publ_id WHERE NOT IN('9038,'9039',例如)。当你在搜索或比较数字或字母串时,你必须使用LIKE。

答案 1 :(得分:0)

Where publ_id not in (90, null);

评估为publ_id&lt;&gt; 90和publ_id&lt;&gt; null未知或未确定,因此您不会获得任何行。 如果你想要没有以90开头的publ_id的行,你可以这样做

Select *  
From publishers
Where publ_id NOT REGEXP '(90)';

SQL Fiddle