语句所用表说明
第二章: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 studentid>6
(3).查询学生表中年龄小于21岁学生所有信息
Select * from studenttable where studentage<21
(4).查询学生表中年龄大于等于23岁学生所有信息
Select * from studenttable where studentage >=23
(5).查询学生表中学号小于等于3的学生所有信息
Select * from studenttable where studentid<=3
(6).查询学生表中学号不大于3的学生所有信息
Select * from studenttable where studentid !>3
(7). 查询学生表中学号不小于3的学生所有信息
Select * from studenttable where studentid!<3
(8). 查询学生表中年龄不是24的学生所有信息
Select * from studenttable where studentage !=24
Select * from studenttable where studentage <>24
3.in运算符
    In运算符是根据给定的值进行查询数据的,它的作用和or的作用是相同的, 但是用起来却比or方便
语法:test_expression [not] insubquery|expression[…n]
(1) 查询图书销售表中图书编号(b_code)为01或者02的记录
Select *from Booksales where b_code in (‘1100010101’,’1100010102’)
(2) 查询图书销售表中图书单价为88的记录
exists子查询
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.between…and not between …and 查指定范围数据
    Between …and 是查询指定范围内的数据,
语法:test_expression [not]between begin_expression and end_expression
(1) 查询学生表中年龄在2023之间的学生姓名和学生年龄
Select studentname as 学生姓名,studentage 学生年龄 from studenttable where studentage between 20 and 23
(2) 查询学生表中年龄不在2023之间的学生姓名和学生年龄
Select studentname ‘学生姓名’,studentage ”学生年龄” from studenttable where studentage not between 20 and 23
(3) 查询图书销售表中销售日期在2010-07-142010-07-16之间的图书编号
Select b_code as 图书编号 from Booksales where b_date between ‘2010-07-14’ and ‘2010-07-16’
(4) 查询图书销售表中销售日期不在2010-07-142010-07-06之间的图书编号
Select b_code as 图书编号 from Booksales where b_date not between ‘2010-07-07-14’ and ‘2010-07-16’]
注意:between…and包含边界值!
5.like和通配符的使用
    Like和通配符是指对数据进行模糊查询
Like谓词只能用于char,nchar,varchar,nvarchar,datetime,这些数据类型
通配符
说明
%
0个或者多个字符组成的任意字符串
_
1个字符组成的任意字符串
[]
[a,f] a或者f的字符串,[a-f]af之接的任意单个字符
[^]
[^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)        查询学生表姓名以ac之接任意字母结尾的学生信息
Select * from studenttable where studentname like ‘%[a-c]’
8    查询学生表姓名不是a或者b或者c开头的所有学生信息
        Select * from studenttable where studentname like ‘[^abc]%’
(9) 查询学生表姓名的开头字母不再af之接的所有学生信息
Select * from studenttable where studentname like ‘[^a-f]%’
(10) 利用escape将通配符转化为实际字符
Select * from studenttable where studentname like ‘%%_’ escape ‘
6利用is nullnot is null查询为空不为空数据及处理空值的函数isnull(),nullif()
空值是未知的值,但空值不包括0,一个或多个空格组成的字符串,或者0长度的字符串,在实际应用中,空值说明还没有像数据库中插入相应的数据。
A. isnull()函数的使用
isnull()函数是将空值处理成指定的值
(1) 将表(contact)中的姓名为杨万波的年龄替换是21
Select id,names,sexs,isnull(ages,21) as ages, phone from contact
B. nullif()函数的使用
nullif()函数是将制定的值处理成空值
(1) 将表(contact)中的年龄为23岁的年龄用空值替换
Select id,names,sexs,nullif(ages,23)as ages,phone from contact
C. is null的使用
(1) 查询表(contact)中的年龄为空的数据
Select * from contact where ages is null
D. is not null的使用
(1) 查询表(contact)中的年龄不为空的数据
Select * from contact where ages is not null
注:isnull()将值替换为空值并不是将原表中的数据更改为空值,而是将查询出来的记录集用中的数据用null替换!nullif()同isnull()一样!