复杂的select语句查询

--复杂的select查询
--1.给列起别名
select stuName as 姓名',--第1种起别名方法,推荐用法
stuNumber 学号,--第2种起别名方法
性别=stuGender --第3种起别名方法 

from TbStudent


--2.select关键字之后,可以跟任何表达式,
--往往用在给查询结果构造-一个表中没有的字段(但它是临时存在的,查询不出来,只是在内存中,不是在库文件里面)
select  stuName, stuNumber, stuGender,是否党员='no' from TbStudent

--3. where子句(条件查询)
--注意: where子句一定要放在被查询的表名后边
select * from TbStudent where  stuGender=0 --判等
一一大小范围比较,关系运算:=,>,<,>=,<=
sel ect*from TbS tudent where stuAge>20 and stuAge<40

--大小范国比較,美系送算: =, >,<,>=,<=, !=(不等于)
select* from TbStudent where stuAge>20 and stuAge<40 and stuAddress=' 幵封
--還揖送算:and,Or,not

select from TbStudent where stuClassId !=2 

--between. . .and. .., between范围比较between. .and. . 是包含这2个数的(是用来比较连续的值)

select from TbStudent where  stuAge between  20 and 40

--查询所有1,2, 3班的学生信息
-很多情况下,一个查询有多重实现方法
select *from TbStudent where stuClassId=1 or stuClassId=2 or stuClassId=3
select *from TbStudent where stuClassId between 1and3

询所有1,3, 4班的学生信息
一一
使用in关键字
sel ect * from TbStudent where stuClassId in(1,3,4)

查询所有1,3, 4班的学生信息
使用in关键字
不在这3个班的同学
select*from TbStudent where stuClassId not in(1, 3, 4)

--top关键字:  查询前若干条几率
select  top(5) * from TbStudent where stuClassId in (1,了, 4)
--查询前百分之几条几率,按百分比查询出于顶端的记录,不按四舍五入,是直接进位
select top 70 percent * from TbStudent

--查询所有的学生籍贯地
select di stinct stuAddress from TbStudent
--distinct:是先从数据表中查询出数据,然后把结果集中完全相同的记录去除掉。只有完全一样才会删除
select DISTINCT stuAddress, stuAge   f rom  TbStudent

--模糊查询:Like
select * from TbStudent where stuName= '刘备 '


通配符:%,_ ,  [] , ^
%通配符:匹配0-n个任意字符
select * from TbStudent where stuName Like '武'

select * from character where tName like '%杰%'(只要出现杰,就行)
--_ 通配符,表示匹配任意1个字符
查找:姓'武',并且名只有1个字的人
select * from TbS tudent where StuName Like '武'

登找:
姓'正,开且名只有1个子的人
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 ' %^敏%' 

聚合函数:不管是多少条记录 最后只能得到1个数

Max:求最大值 ;select MAX(tAge)from character

min:求最小值 ;select MIN(tAge)from character

count:计算记录条数;;select COUNT(*)from character
                        select COUNT(1)from character

                     (如果要统计的那条记录我Null,则不计算在内。

                            推荐使用主键或者1作为count()对象)

sum:求和;select SUM(tAge)from character

avg:求平津值;select avg(tAge)from character

猜你喜欢

转载自blog.csdn.net/cc1179877179/article/details/81267646