`

EXISTS AND NOT EXISTS

阅读更多
EXISTS或者NOT EXISTS是把主查询的字段传到后边的查询中作为条件,返回值是TRUE或者FALSE。EXISTS TRUE,那么就是查询条件成立,结果会显示出来。NOT EXISTS TRUE,则为FALSE,查询连接条件不成立。
select * from course where not exists(select * from grade where grade.课程代号=course.课程代号)     
这个语句,是查询course表中课程代号在grade中没有出现的数据。
看看grade表,课程编号有K01到K06,而COURSE表,有K01到K07,那么K07在GRADE表是不存在的,那么,是符合条件的。
同样select * from course where exists(select * from grade where grade.课程代号=course.课程代号) 
则是查询COURSE的记录条件为编号在GRADE中存在。那么很明显,结果是K01到K06的数据。
另外,EXISTS和NOT EXISTS的作用可以用IN或NOT IN实现,但是,效率要高。
因为EXISTS和NOT EXISTS返回的结果是TRUE或者FALSE,那么则在子查询中,遇到第一个符合条件的结果,就会退出查询,而不会进行全表的检索。而NOT IN或者IN,要把子查询中的SELECT字句全部查询出来才行。
分享到:
评论

相关推荐

    mssql和sqlite中关于if not exists 的写法

    在sql语名中,if not exists 即如果不存在... 代码如下:if not exists (select * from sysobjects where id = object_id(‘table_name’) and OBJECTPROPERTY(id, ‘IsUserTable’) = 1) c,判断列不存在 代码如下:if

    egg-upload:egg upload file demo (form 表单)

    uploadQuickStartsee for more detail.Development$ npm i$ npm run dev$ open http://localhost:7001/Deploy$ npm start$ npm stopnpm scriptsUse npm run lint to check code style.Use npm test to run unit test...

    pb学生成绩管理系统

    if exists(select 1 from sys.sysforeignkey where role='FK_CHOOSE_C_CHOOSE_CO_COURSES') then alter table choose_course delete foreign key FK_CHOOSE_C_CHOOSE_CO_COURSES end if; if exists(select 1 from...

    SQL SERVER先判断视图是否存在然后再创建视图的语句

    IF NOT EXISTS(SELECT 1 FROM sys.views WHERE name='Report_IndividualTicket') BEGIN create view Report_IndividualTicket as SELECT Ticket.TicketNumber, Ticket.TicketID, GisProcess.StageName, Content....

    Feature Extraction and Image Processing

    combines to show students, in lectures and in study, not only how techniques are implemented, but also how and why they work with an explicit relation to conventional teaching material.

    sql查询语句

    select sc.* from sc where not exists( select cno from c where( sc.cno!='c1' and sc.cno!='c2' ) ) --其中not exists 中的 select cno from c where 是没有用的 --等价于 select sc.* from sc where cno not in...

    AD HOC NETWORKS USING DIRECTIONAL ANTENNAS AND POWER CONTROL

    The potential for deployment of such networks exists in many scenarios ranging from civil and construction engineering, and disaster relief to sensor networks and military applications. The IEEE 802....

    人事管理信息系统vb+sql

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tbUser]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[tbUser] GO if exists (select * from dbo.sysobjects where ...

    Jitter Transfer Characteristics of Delay-Locked

    -domain model, we showthat in a widely used DLL configuration,jitter peaking always exists and high-frequency jitter does not get attenuated as previous analyses suggest. This is true even in a first ...

    Visual Knowledge Discovery and Machine Learning

    Such methods do not represent the n-D data fully and do not allow the restoration of the n-D data completely from their 2-D representation. Respectively, our abilities to discover the n-D data ...

    Data Fusion- Concepts and Ideas

    In a few places, where such code is not readily available, we have included Matlab code in the body of the text. Layout. The layout and typography has been revised. Examples and Matlab code now ...

    linux-x86_64_ccp4-7.0-setup-linux64.tar.gz

    CCP4 exists to produce and support a world-leading, integrated suite of programs that allows researchers to determine macromolecular structures by X-ray crystallography, and other biophysical ...

    Motion Segmentation and Depth Ordering Using an Occlusion Detector

    we also include cases where no intensity edge exists at the location of the motion boundary, or when no parametric motion model can describe the data. Finally, we describe psychophysical experiments ...

    squashfs1.3r3.tar.gz

    can be used to force mksquashfs to not compress inodes/directories and data respectively. Giving both options generates an uncompressed filesystem. The -le and -be options can be used to force ...

    解析PHP中empty is_null和isset的测试

    本篇文章是对PHP中empty is_null和isse的测试进行了详细的分析介绍,需要的朋友参考下

    A Markov Chain Monte Carlo Method for Inverse Stochastic Simulation

    method: the spatial structure and statistics are not preserved during the procedure of model calibration and history matching. While the spatial structure and statistics of models may be one of the ...

    数据库图书馆课程设计

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_Borrow_Info_Book_Info]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[Borrow_Info] DROP CONSTRAINT FK_Borrow...

    oracle中通配符和运算符的使用方法介绍

     包含:in、not in exists、not exists  范围:between…and、not between….and  匹配测试:like、not like  Null测试:is null、is not null  布尔链接:and、or、not  通配符:  在where子句中,...

    Linear Programming Approach for Probabilistic Robot Path Planning

    In practical robot motion planning, robots usually do not have full models of their surrounding, and hence no complete and correct plan exists for the robots to be executed fully. In most real-world ...

Global site tag (gtag.js) - Google Analytics