现在的位置: 首页 > 综合 > 正文

Sql Server 学习笔记三

2013年10月13日 ⁄ 综合 ⁄ 共 881字 ⁄ 字号 评论关闭

SQL的通配符的使用 

"_",指一个字符
select * from T_person4  where Name like '_大'
"%" 零个或任意多大字符

select * from T_person4   where Name like '李%'     以“李”开头的

 

空值的处理

NULL 不是为空,而是不知道为何值

select * from T_person4 where Name=null

查询为空

select null+1 也是没有数据,NULL为不知道,所以查询的时候也是不知道

select * from T_person4   where Name is null  (is null) --取出为NULL的值

select * from T_person4   where Name is null  (is not null) --取出不为NULL的值

 

取范围

select * from T_person4  where Age=22 or Age=42

select * from T_person4   where Age in(22,42)

select * from T_person4  where Age>20 and Age<30

 

数据分组

 

select Age,count(*) from T_person4   Group by Age-------先按年龄分组,再统计每组人的个数。

select Age,max(Fsalary) as '最大工资',count(*) from T_person4   Group by Age  // 没有放在Group by里面的列,不能放在select 里面,除了聚合函数。

 

Group by 也是放在WHERE 之后,说明执行是在WHERE 之后的

Having 子句

1.where 不能使用聚合函数

select Age,max(Fsalary) as '最大工资',count(*) from T_person4
Group by Age
Having count(*)>1

Having 出现在Group by之后的,对分组后的值进行过滤,能用的列和SELECT中能用的列是一样的。

必须是放在SELECT里面,或者聚合函数、

where 对原始数据进行过滤,Having对分组后的值进行过滤。

 

 

抱歉!评论已关闭.