设为首页 加入收藏

TOP

史上最全的MSSQL复习笔记(四)
2015-11-21 02:09:59 来源: 作者: 【 】 浏览:5
Tags:史上 最全 MSSQL 复习 笔记
where Sex='女' and BornDate >'1990-1-1' and Address='广州传智播客'
--指定区间范围
select StudentNo,StudentName,Sex,BornDate,Address from Student where ?BornDate >='1990-1-1' and BornDate<='1993-1-1'
--between...and ?>= ?<=
select StudentNo,StudentName,Sex,BornDate,Address from Student where BornDate ?between '1990-1-1' and '1993-1-1'
--查询班级id ?1 ?3 5 ?7的学员信息
select * from Student where ClassId=1 or ClassId=3 or ClassId=5 or ClassId=7
--指定具体的取值范围--可以是任意类型的范围.值的类型需要一致--可以相互转换
select * from Student where ClassId in(1,3,'5',7)
select * from Student where ClassId not in(1,3,'5',7)
?
?
?
--带条件的查询-模糊查询-- 只针对字符串而言
?
--查询 ?姓 林 的女生信息
--=是一种精确查询,需要完全匹配
select * from Student where Sex='女' and StudentName='林'
--通配符--元字符
--%:任意个任意字段 ?window:* ?正则表达式 :.*
--_:任意的单个字符
--[]:代表一个指定的范围,范围可以是连续也可以是间断的。与正则表达式完全一样[0-9a-zA-Z].可以从这个范围中取一个字符
--[^]:取反值
select * from Student where Sex='女' and StudentName='林%'
--通配符必须在模糊查询关键的中才可以做为通配符使用,否则就是普通字符
--like ? 像 。。。。一样
select * from Student where Sex='女' and StudentName ?like '林%'
select * from Student where Sex='女' and StudentName ?like '林_'
--[]的使用 ?学号在11~15之间的学员信息
select * from Student where StudentNo like '[13579]'
?
---处理null值
--null:不是地址没有分配,而是不知道你需要存储什么值 ?所以null是指 ? 不知道。但是=只能匹配具体的值,而null根本就不是一个值
select COUNT(email) from Student where Email !=null
select COUNT(email) from Student where Email ?is null
select count(email) from Student where Email ?is not null
--将null值替换为指定的字符串值
select StudentName,ISNULL(Email,'没有填写电子邮箱') from Student where ClassId=2
?
?
?
--当你看到 ?每一个,,各自,不同,,分别 ?需要考虑分组
--查询每一个班级的男生人数
--与聚合函数一起出现在查询中的列,要么也被聚合,要么被分组
select classid,Sex,COUNT(*) from Student where Sex='男' group by ClassId,sex
--查询每一个班级的总人数,显示人数>=2的信息
--1.聚合不应出现在 WHERE 子句中--语法错误
select ClassId ,COUNT(*) as num from Student where Email is not null ? GROUP by ClassId having COUNT(*)>=2 order by num desc
--完整的sql查询家庭
?--5 ? ? ? ? ? ? ? ? ? ? ? ? ? ?1 ? ? ? ? ? ? ? ? ? ? ?2 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 4 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 6 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
--select 字段列表 from 表列表 ?where 数据源做筛选 group by 分组字段列表 having 分组结果集做筛选 Order by ?对结果集做记录重排
?
select ClassId ,COUNT(*) as num from Student where Email is not null ? GROUP by ClassId order by ClassId desc
?
--关于top的执行顺序 排序之后再取top值
select top 1 ClassId ,COUNT(*) as num from Student ?GROUP by ClassId ?order by num desc
?
?
7.类型转换函数
?
--select :输出为结果集--虚拟表
--print:以文本形式输出  只能输出一个字符串值.

print 1+'a'
select 1,2

select * from Student

--类型转换
--Convert(目标类型,源数据,[格式]) --日期有格式
print '我的成绩是:'+convert(char(3),100)

print '今天是个大日子:'+convert(varchar(30),getdate(),120)
select getdate()
select len(getdate())

--cast(源数据  as  目标类型)  它没有格式
print '我的成绩是:'+cast(100 as char(3))

?

?
?
8.日期函数
?
?
--getdate():获取当前服务器日期
select GETDATE()
--可以在源日期值是追加指定时间间隔的日期数
select DATEADD(dd,-90,GETDATE())
--dateDiff:找到两个日期之间指定格式的差异值
select StudentName,DATEDIFF(yyyy,getdate(),BornDate) as age from Student order by ?age
--DATENAME:可以获取日期的指定格式的字符串表现形式
select DATENAME(dw,getdate())
--DATEPART:可以获取指定的日期部分
select cast(DATEPART(yyyy,getdate()) as CHAR(4))+'-' +cast(DATEPART(mm,getdate()) as CHAR(2))+'-' +cast(DATEPART(dd,getdate()) as CHAR(2))
?
?
9.数学函数
?
?
--rand:随机数:返回0到1
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 4/10/10
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇SQL存储过程将符合条件的大量记录.. 下一篇SQL 查询所有表名、字段名、类型..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: