select * from TbStudent where stuName='安飞'

 

--通配符:%,_,[],^

--%通配符:匹配0-n个任意字符

--_通配符,表示匹配任意一个字符

--查询所有姓‘安’的同学信息

select * from TbStudent where stuName like '安%'

--查找:姓’安‘,并且名字只有一个子的人

select * from TbStudent where stuName like '安_'

--查找:名字里面有’飞‘字的学生信息

select * from TbStudent where stuName like '%飞%'

--查找:名字里面没有’安‘字的学生信息

select * from TbStudent where stuName not like '%安%'

--通配符’^‘:非,不是

select * from TbStudent where stuName like '%^安%'   --?×

 

 

--'[]'通配符,表示匹配一定范围之内的,任意一个字符

 

--查询:姓’安‘或者姓’飞‘的所有记录

select * from TbStudent where stuName like '安%' or stuName like '飞%'

 

select * from TbStudent where stuName like '[安飞]%'

 

select * from TbStudent where stuName like '[安,飞]%'

 

--练习:查询12-14级的所有学生信息

--错误做法:select * from TbStudent where StuNumber like '[12,13,14]'

--通配符[],永远只去匹配一个字符

select * from TbStudent where StuNumber like '1[2-4]'

select * from TbStudent where StuNumber like '1[2,3,4]'

--查询不是14级学生

select *from TbStudent where stuNumber not like '14%'

--^通配符:必须用在[]里,也就是必须和通配符[]搭配使用

select *from TbStudent where stuNumber not like '1[^4]%'

 

--^不是标准SQL通配符,它只是 MS SQL Server支持的,其他DBMS都不支持该符号

--我们一般优先使用not like ,尽量不用^

 

--练习:查询名字里面没有’敏‘字的学生

select * from TbStudent where stuName not like '%敏%'

--错误:select * from TbStudent where stuName like '%[^敏]%'

--查询不姓武的

select * from Tbstudent where stuName like '[^武]%'

 

 

--匹配特殊字符:使用[ ]做转义:[%]

 --加入人名里面有’%‘

 --查询名字里有’%‘的学生

 select * from TbStudent where stuName like '%[%]%'

 

 

--聚合函数

--max(),min(),avg(),sum(),count()

--MAX()求最大值

select MAX(stuAge) from TbStudent

 

--MIN()求最小值

select MIN(stuAge) from TbStudent

 

--AVG()求最平均值

select AVG (stuAge) from TbStudent

 

--SUM()求最和

select SUM (stuAge) from TbStudent

 

--COUNT()统计记录条数

select COUNT (*) from TbStudent

select COUNT (stuId) from TbStudent

select COUNT (stuName) from TbStudent

select COUNT (1) from TbStudent

select COUNT (stuAge) from TbStudent

--count()的字段如果为null,是不会被计算在内的

--count()记录条数是,推荐使用主键或者1,作为count对象,坚决不能使用*

--avg()时,如果记录在该字段上为null,这条记录是不你会被通缉在内的

 

--这五个为什么叫做聚合函数

--不管是多少条记录,经过这五个函数的运算后,都只能得到一个数,所以他们叫聚合函数

--注意:

select stuName,AVG(stuAge) from TbStudent  ×--不能这样使用,前者为一串数据,后者为一个数

select MAX(stuAge) as 最大值,MIN(stuAge) as 最小值,AVG(stuAge) as 平均值

from TbStudent