SQL 运算符及模糊查询,空字段的处理等等..doc_第1页
SQL 运算符及模糊查询,空字段的处理等等..doc_第2页
SQL 运算符及模糊查询,空字段的处理等等..doc_第3页
SQL 运算符及模糊查询,空字段的处理等等..doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

语句所用表说明第二章:select 语句:where子句过滤1.And 和 or 运算符and运算符用于连接两个布尔型表达式,当有所有表达式都为true时返回true,当有一个表达式返回为false时则返回false语法:Boolean_experssion and Boolean_experssion(1).利用图书销售表查询图书编号是1100010101和销售数量为2的图书单价Select b_price from Bookinfo where b_code=1100010101 and b_number = 2 or运算符也是用于连接两个布尔型表达式,但是当表达式有一个为flase则返回false,语法:Boolean_experssion or Boolean_experssion(1).利用图书信息表(Bookinfo)查询图书编号(b_code)是1100010101或者是1100010102的所有图书信息Select * from Bookinfo where b_code=1100010101 or b_code = 1100010102注意:and优先级大于or优先级,要想改变优先级的级别,则用小括号括起来2.比较用算符比较用算符:运算符说明=等于号大于号=大于等于号不大于!不小于或者!=不等于(1).查询学生表中年龄为22学生的所有信息Select * from studenttable where studentage = 21(2).查询学生表中学号大于6的学生所有信息Select * from studenttable where studentid6(3).查询学生表中年龄小于21岁学生所有信息Select * from studenttable where studentage=23(5).查询学生表中学号小于等于3的学生所有信息Select * from studenttable where studentid3(7). 查询学生表中学号不小于3的学生所有信息Select * from studenttable where studentid!3(8). 查询学生表中年龄不是24的学生所有信息Select *from studenttable where studentage !=24Select *from studenttable where studentage 243.in运算符 In运算符是根据给定的值进行查询数据的,它的作用和or的作用是相同的, 但是用起来却比or方便语法:test_expression not in(subquery|expressionn)(1) 查询图书销售表中图书编号(b_code)为01或者02的记录Select *from Booksales where b_code in (1100010101,1100010102)(2) 查询图书销售表中图书单价为88的记录Select * from Booksales where b_price in (66+22)(3) 查询图书销售表中图书单价为59.8的记录Select * from Booksales where 59.8 in (b_price)(4) 查询图书销售表中图书编号不为01或者02的记录Select * from Booksales where b_code not in (1100010101,1100010102)(5) 查询图书销售表中图书单价不为88或者58.8记录Select * from Booksales where b_price not in (77+11,58+0.8)(6) 查询图书销售表中图书单价不为88的记录Select * from Booksales where 88 not in (b_price)4.betweenand 和not between and 查找指定范围数据 Between and 是查询指定范围内的数据,语法:test_expression notbetween begin_expression and end_expression(1) 查询学生表中年龄在20到23之间的学生姓名和学生年龄 Select studentname as 学生姓名,studentage 学生年龄 from studenttable where studentage between 20 and 23(2) 查询学生表中年龄不在20到23之间的学生姓名和学生年龄Select studentname 学生姓名,studentage ”学生年龄” from studenttable where studentage not between 20 and 23(3) 查询图书销售表中销售日期在2010-07-14到2010-07-16之间的图书编号 Select b_code as 图书编号 from Booksales where b_date between 2010-07-14 and 2010-07-16(4) 查询图书销售表中销售日期不在2010-07-14到2010-07-06之间的图书编号 Select b_code as 图书编号 from Booksales where b_date not between 2010-07-07-14 and 2010-07-16注意:betweenand包含边界值!5.like和通配符的使用Like和通配符是指对数据进行模糊查询Like谓词只能用于char,nchar,varchar,nvarchar,datetime,这些数据类型通配符说明%由0个或者多个字符组成的任意字符串_由1个字符组成的任意字符串a,f a或者f的字符串,a-fa到f之接的任意单个字符a,f不是a或者f的字符串,a-f不是a-f之接的任意字符串(1)查询学生表姓名以杨开头的所有信息 Select * from studenttable where studentname like 杨%(2)查询学生表姓名以盼结尾的所有信息Select * from studenttable where studentname like %盼(3) 查询学生表姓名以杨开头后面是任意一个字符的所有信息Select * from studenttable where studentname like 杨_(4) 查询学生表姓名以博字结尾姓名是两个字符的所有信息Select *from studenttbale where studentname like _博(5) 查询学生表姓名以杨或者王开头的两个字符的所有信息Select * from studenttable where studentname like 杨,王_(6) 查询学生表姓名以a或者c开头的学生所有信息Select * from studenttable where studentname like a,c%(7)查询学生表姓名以a到c之接任意字母结尾的学生信息Select * from studenttable where studentname like %a-c(8)查询学生表姓名不是a或者b或者c开头的所有学生信息Select * from studenttable where studentname like abc%(9) 查询学生表姓名的开头字母不再a到f之接的所有学生信息Select * from studenttable where studentname like a-f%(10) 利用escape将通配符转化为实际字符Select * from studenttable where studentname like %胖%_ escape 胖6利用is null和not is null查询为空不为空数据及处理空值的函数isnull(),nullif()空值是未知的值,但空值不包括0,一个或多个空格组成的字符串,或者0长度的字符串,在实际应用中,空值说明还没有像数据库中插入相应的数据。A. isnull()函数的使用isnull()函数是将空值处理成指定的值(1) 将联系方式表(contact)中的姓名为杨万波的年龄替换是21Select id,names,sexs,isnull(ages,21) as ages, phone from contactB. nullif()函数的使用nullif()函数是将制定的值处理成空值(1) 将联系方式表(contact)中的年龄为23岁的年龄用空值替换Select id,names,sexs,nullif(ages,23)as ages,phone from contactC. is null的使用(1) 查询联系方式表(contact)中的年龄为空的数据Select * from contact where ages is nullD. is not null的使用(1) 查询联系方式表(contact)中的年龄不为空的数据Select * from contact where ages is not null注:isnull()将值替换为空值并不是将原表中的数据更改为空值,而是将查询出来的记录集用中的数据用null替换!nullif()同isnull()一样!如果要将查询的结果集生成真的表则可以Select id,names,sexs,isnull(ages,21)as ages,phone into newcontact from contact7.利用谓词any或者all查询数据All:代表子查询中的每一个,是且的关系,取两个值Any:代表子查询中的每一个值,是或的关系,取一个值例如:Select * from Bookinfo where b_price all(select b_price from Bookinfo where b_author in (郭盼,雷雨年)就是查询图书价格大于作者是郭盼并且大于雷雨年的所有图书信息,也就是大于最大值Select * from Bookinfo where b_price any(select b_price from Bookinfo where b_author in (郭盼,雷雨年)就是查询图书价格大于作者是郭盼或者大于雷雨年的所有图书信息,也就是大于最小值(1)查询图书价格大于作者是雷雨年,郭盼的图书信息Select *from Bookinfo where b_price all (select b_price from Bookinfo where b_author in (雷雨年,郭盼)(2)查询图书价格大于作者是雷雨年或者郭盼的图书信息Select * from Bookinfo where b_priceany(select b_price from Bookinfo where b_author in (雷雨年,郭盼)8.exists的用法在一些情况下,只要子查询返回一个真值或者假值,只考虑是否满足谓词条件,数据内容本身并不重要的情况下,可用Exists来定义子查询,如果子查询返回一行或者多行,exists谓词为真否则为假,要使exists谓词有用,应该在自查询中建立查询条件以匹配自查询连接起来的两个表中的值!语法:exists subquery例:(1)子查询中查询null返回结果为trueSelect * from Bookinfo where exitst(select null)(2) 查询图书

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论