SQL学习笔记
1、select SAge,SName from Student where SName='aaa';
select SAge,SName from Student where SAge=21 order by SName;
单括号用来限定字符串,如果将值与串类型进行比较,则需要限定单引号
2、同时使用order by 和where时order by应该放在where 后面!否则将有错误产生!
3、用非检索的列排序数据或过滤数据是完全可以的.例:
select SName from Student where SAge<=21;其中Sage并不在检索的列中!
4、select SAge,SName from Student where SAge between 21 and 23;
select SAge,SName from Student where not (SAge between 21 and 23);
5、在搜索中(%)表示任何字符出现任意次数(access用*号)。而(_)通配符只匹配单个字符而不是多个字符(access用?号)。
select * from Student where SAddress like '%hen%';
6、方括号([])通配符用来指定一个字符集,它必须匹配指定位置的一个字符。
例:select * from Student where SName like '[ha]%';是检索出所有名字以’h’或’m’开头的联系人。
7、脱字符(^)表示否定集合
例:select * from Student where SName like '[^ha]%';是检索出所有名字不以’h’或’m’开头的联系人。
select * from Student where not SName like '[ha]%';效果相同
8、trim()函数去掉串左右两边的所有空格。rtrim()函数去掉串右边的所有空格。ltrim()函数去掉串左边的所有空格。
9、拼接字段和使用别名。select SName+' ('+SAddress+')' as person from Student;
执行算术计算和使用别名。select SName,SAge,SAge-Number as age from Student ;
10、sqlserver查询字符串的长度用len()而不用length()
11、日期函数的可移植性非常差!
12、变量必须用@开头. @ 局部变量,@@ 全局变量
select @age=avg(SAge) from Student
print @age
set @time = getdate() --get current time
set @date = cast(@time as datetime) --cast time to date
print @time
print @date
13、ave()函数忽略列值为NULL的行。
‘ ‘和NULL是不同的。
select avg(SAge) as s_age from Student
14、group by 必须出现在where子句之后,oreder by 之前。
Where在数据分组前进行过滤,having 在数据分组之后进行过滤。
15、除聚集计算外select语句中的每个列都必须在group by子句中给出,
group by 中列出的列须使是检索列或有效的表达式,不能使用别名(as指定的名)。
Having不能使用别名(as指定的名)
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号