码迷,mamicode.com
首页 > 数据库 > 详细

第17课-数据库开发及ado.net 聚合函数,模糊查询like,通配符.空值处理.order by排序.分组group by-having.类型转换-cast,Convert.union all; Select 列 into 新表;字符串函数;日期函数

时间:2016-08-11 00:45:48      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

17-数据库开发及ado.net

聚合函数,模糊查询like,通配符.空值处理.order by排序.分组group by-having.类型转换-cast,Convert.union all;  Select into 新表;字符串函数;日期函数

SQL聚合函数

MAX(最大值)、MIN(最小值)、AVG(平均值)、SUM(和)、COUNT(数量:记录的条数)

聚合函数对null不计算。如果一行数据都是null,count(*)包含对空值行、重复行的统计。

--聚合函数演示

select * from TblStudent

--cast convert ???

--平均年龄

select AVG(tsage*1.0) as 平均值 from TblStudent

select AVG(tsage*1.0) 平均值 from TblStudent

select 平均值=AVG(tsage*1.0)  from TblStudent

--报错.当使用聚合函数的时候,注意在select查询列表中不要出现除了使用聚合函数以外的其他列,

--除非该列也使用了聚合函数或者该列包含在了Group By子句中。

select 平均值=AVG(tsage*1.0),tSName from TblStudent

--求和

select SUM(tsage) from TblStudent

--求最大值

select MAX(tsage) from TblStudent

--最小值

select min(tsage) from TblStudent

--总条数

select count(*) from TblStudent

--将tSId=32和tSId=30 tsage=null

update TblStudent set tSAge=null where tSId=32 or tSId=30

--

select COUNT(tsage) as 记录条数 from TblStudent

 

select

   COUNT(tsage),

   SUM (tsage),

   MAX (tsage),

   MIN (tsage),

   AVG(tsage)

   from tblstudent

  

  

select * from TblStudent--主键表

select * from TblScore--外键表

--查询英语没有及格的学生的学号

select tsid from TblScore where tEnglish <60

--查询年龄在-30岁之间的男学生

select * from TblStudent  where tsage<=30 and tsage>=20 and tSGender =‘男‘

--not and or 是个逻辑运算符,优先级not→and→or

 

--Between...and...在之间

--查询年龄在-30岁之间的男学生

select * from TblStudent where tSAge between 20 and 30

--查询Math成绩在-70分之间的所有学生

select tsid from TblScore where tmath between 65 and 70

--查询班级ID为,2,3的所有学生

select * from TblStudent where tSClassId in (1,2,3)

select * from TblStudent where tSClassId =1 or tSClassId =2 or tSClassId =3

--如果上面的写法可以优化为下面的这种写法,则尽量用下面这种写法.

select * from TblStudent where tSClassId >=1 and tSClassId <=3

模糊查询(都是针对字符串操作的)

--模糊查询,通配符

select * from TblStudent

--之前的查询使用:=

select * from TblStudent where tSName =N‘张犇‘

--第一个通配符% 表示任意多个任意字符。

--当使用通配符来匹配的时候必须使用Like

select * from TblStudent where tSName like N‘张%‘

--通过[]将%包含起来,则%不在是表示一个通配符,而是表示一个普通的字符

select * from TblStudent where tSName like N‘%[%]%‘

--通配符:_ 表示(单个)一个任意字符。

select * from TblStudent  where tSName like N‘貂_‘

select * from TblStudent where tSName like N‘貂%‘ and LEN(tSName)=2

--通配符:[]

select * from TblStudent  where tSName like N‘张_妹‘

select * from TblStudent  where tSName like N‘张[0-9]妹‘

select * from TblStudent  where tSName like N‘张[a-z]妹‘

--like 和 not like  [^] 通配符的使用注意区分

select * from TblStudent where tSName like N‘张_妹‘

select * from TblStudent where tSName not like N‘张_妹‘

select * from TblStudent where tSName not like N‘张[^0-9]妹‘

空值处理

--空值处理

--null

select * from TblStudent

update TblStudent set tSName =null where tSId=1 --表示数据库的空值

update TblStudent set tSName =‘‘ where tSId=2 --表示一个长度为零的字符串

--请查询出所有tsage为null的学生记录(以下两种都是错误的。)

select * from TblStudent where tSAge =null

select * from TblStudent where tSAge <>null

--数据库中的null值比较特殊,表示一个unknow的值。不知道的值。

--在数据库中对NULL值不能用=或<>来判断,要判断null值,只能使用一个特殊的运算符IS来判断。

select * from TblStudent where tSAge is  null

select * from TblStudent where tSAge is not null

--isnull(),表示一个函数区别以上的is null

--null与任何运算结果都是NULL

 

第17课-数据库开发及ado.net 聚合函数,模糊查询like,通配符.空值处理.order by排序.分组group by-having.类型转换-cast,Convert.union all; Select 列 into 新表;字符串函数;日期函数

标签:

原文地址:http://www.cnblogs.com/Time_1990/p/5759193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!