位置: 编程技术 - 正文

sqlserver exists,not exists的用法

编辑:rootadmin
学生表:create table student( id number(8) primary key, name varchar2(),deptment number(8))选课表:create table select_course( ID NUMBER(8) primary key, STUDENT_ID NUMBER(8) foreign key (COURSE_ID) references course(ID), COURSE_ID NUMBER(8) foreign key (STUDENT_ID) references student(ID))课程表:create table COURSE( ID NUMBER(8) not null, C_NAME VARCHAR2(), C_NO VARCHAR2())student表的数据: ID NAME DEPTMENT_ID---------- --------------- ----------- 1 echo 2 spring 3 smith 4 liter course表的数据: ID C_NAME C_NO---------- -------------------- -------- 1 数据库 data1 2 数学 month1 3 英语 english1select_course表的数据: ID STUDENT_ID COURSE_ID---------- ---------- ---------- 1 1 1 2 1 2 3 1 3 4 2 1 5 2 2 6 3 .查询选修了所有课程的学生id、name:(即这一个学生没有一门课程他没有选的。)分析:如果有一门课没有选,则此时(1)select * from select_course sc where sc.student_id=ts.id and sc.course_id=c.id存在null,这说明(2)select * from course c 的查询结果中确实有记录不存在(1查询中),查询结果返回没有选的课程,此时select * from t_student ts 后的not exists 判断结果为false,不执行查询。SQL> select * from t_student ts where not exists (select * from course c where not exists (select * from select_course sc where sc.student_id=ts.id and sc.course_id=c.id)); ID NAME DEPTMENT_ID---------- --------------- ----------- 1 echo .查询没有选择所有课程的学生,即没有全选的学生。(存在这样的一个学生,他至少有一门课没有选),分析:只要有一个门没有选,即select * from select_course sc where student_id=t_student.id and course_id=course.id 有一条为空,即not exists null 为true,此时select * from course有查询结果(id为子查询中的course.id ),因此select id,name from t_student 将执行查询(id为子查询中t_student.id )。SQL> select id,name from t_student where exists(select * from course where not exists(select * from select_course sc where student_id=t_student.id and course_id=course.id)); ID NAME---------- --------------- 2 spring 3 smith 4 liter3.查询一门课也没有选的学生。(不存这样的一个学生,他至少选修一门课程),分析:如果他选修了一门select * from course结果集不为空,not exists 判断结果为false;select id,name from t_student 不执行查询。SQL> select id,name from t_student where not exists(select * from course where exists(select * from select_course sc where student_id=t_student.id and course_id=course.id)); ID NAME---------- --------------- 4 liter4.查询至少选修了一门课程的学生。SQL> select id,name from t_student where exists(select * from course where exists(select * from select_course sc where student_id=t_student.id and course_id=course.id)); ID NAME---------- --------------- 1 echo 2 spring 3 smith

推荐整理分享sqlserver exists,not exists的用法,希望有所帮助,仅作参考,欢迎阅读内容。

sqlserver exists,not exists的用法

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

sqlserver substring函数使用方法小结 在操作sqlserver时候用到了substring函数SUBSTRING(expression,start,length)参数expression字符串、二进制字符串、文本、图像、列或包含列的表达式。请勿使用包含聚

sql 判断数据库,表,存储过程等是否存在的代码 sql判断是否存在--判断数据库是否存在ifexists(select*frommaster..sysdatabaseswherename=N'库名')print'exists'elseprint'notexists'-----------------判断要创建的表名是否存

做购物车系统时利用到得几个sqlserver 存储过程 即以游客身份登录网站时以cookie的方式存储购物车,而以登录用户的身份进入时将购物车信息存储到数据库中去,若是先以游客身份完成购物再登录继

标签: sqlserver exists,not exists的用法

本文链接地址:https://www.jiuchutong.com/biancheng/349787.html 转载请保留说明!

上一篇:SQL 根据汉字获取全拼的代码(sql查询字段值为汉字的)

下一篇:sqlserver substring函数使用方法小结

  • 招待费增值税税率
  • 一般纳税人金税盘抵税会计分录
  • 模具费怎么入账
  • 如何保证企业生存发展
  • 增值税进项税转出文件
  • 金融行业打包是什么意思
  • 资产损失税前扣除管理办法2019
  • 不动产在建工程使用的外购物资可以抵扣吗
  • 社保可以在税前扣除吗
  • 住宿费专用发票会计分录
  • 无偿赠与钱要交税吗
  • 支付工人保险费计入什么科目里
  • 电子承兑到期提示付款选择线上还是线下
  • 如何核算卷烟消费税税率?
  • 合同成本对应科目
  • 应收账款坏账有什么措施改善
  • 个人出租住房需要交城镇土地使用税吗
  • 票据贴现公司需要资质吗
  • 金融服务的手续费要交税吗
  • 树木种植公司
  • 福利费如何做会计分录
  • macos big sur使用技巧
  • 废品损失的核算方式主要有
  • 会计科目明细分类科目表
  • php fgets函数的用法
  • 预收账款的账务处理摘要
  • 华为2022年资产负债表数据
  • php模板引擎原理
  • 营改增后一般纳税人缴纳增值税彩用简易征收3%
  • 增值税专用发票查询系统官方网站
  • phpcms 还有人用吗
  • 进项税已认证未缴纳
  • 大前端2021
  • 前端页面设计
  • apache php mysql开发环境安装教程
  • map获取entry
  • kill命令用法
  • 查看zip内容
  • 原始凭证必要时可以涂改
  • 不确认收款退款对方还能收到钱么?
  • 0申报残保金逾期影响企业信用评级吗为什么
  • 进项税额加计扣除5%账务处理
  • SQLServer 2008 R2中使用Cross apply统计最新数据和最近数据
  • mongodb安装教程图解
  • 歌咏比赛服装费用规定标准最新
  • db2 connect命令
  • 上年度企业所得税调整需要调账吗
  • 发票入账需要哪些附件的文件
  • 定额备用金与非备用金
  • 单位租的个人房怎么入账
  • 应付票据转应付账款有附件吗
  • 土地增值税预缴计算方法70号公告
  • 公司发放工作服怎么做账务处理
  • 待认证进项税额借贷方向
  • 报销差旅费用什么收款凭证还是转账凭证
  • 企业购买的土地计入什么科目
  • 清洁服务公司账务处理
  • 财务费用怎么计入现金流量表
  • sql server 2019自定义安装教程
  • 分析sql语句性能实现
  • mysql 5.7启动
  • bios是什么意思的
  • 如何用u盘在车上放音乐
  • win10怎么合并磁盘空间
  • 用户账户限制
  • windows资源监视器
  • win7开机一直显示配置windows请勿关机怎么办
  • linux文件复制到windows
  • linux 有哪些
  • WIN7系统的镜像文件在哪里
  • javaScript基础语法数据类型
  • xml文件网络传输
  • jQuery position() 函数详解以及jQuery中position函数的应用
  • easyui messager alert 三秒后自动关闭提示的实例
  • jquery写下拉框
  • 美国税务制度
  • 软件开发交流平台
  • 怎样在中国税务用手机交社保
  • 青岛税务局局长是什么级别?
  • 税务师 领证
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设