sql常用总结

 

delete  from xx  是删除表中是数据
而drop table xx  是把表直接删除掉

@@Version 查询当前数据库的版本信息
getdate() 系统当前时间

聚合函数
count(*)总条数
max()最大
min()最小
avg()平均值
sum()和

排序
order by 从小到达排序
如 select * from 表名 order by 字段名 ASC
降序为
select * from 表名 order by 字段名 desc
如果想按两个字段进行排序
select * from 表名 order by 字段名 desc,字段名Asc

通配符查询
单字符通配符
_代表一个字符

多字符通配符
%n%

SQL中使用is null、is not null 来进行空值判断


group by 分组
如select age,count(*) from table group by age
根据年龄进行分组,并且每个年龄有多少人

top xx 就是前几行

去重复
distinct 完全去重复

union all 不去除重复
ABS() 求绝对值
LEN() 计算字符串长度
LOWER()、UPPER() 转小写、大写
LTRIM()
RTRIM()
SUBSTRING(STRING,START_POSITION,LENGTH)
参数string为主字符串,start_position为子字符串在主字符串中的其实位置,length为子字符串的最大长度。 

isnull(如果不为空选择这个,'否则是这个值')

离散值判断
case i
when 1 then aa
when 2 then bb
else
end


 练习题
表中有 ABC三列,用sql语句实现:当A列大于b列时选择a列否则选择b列,当b列
大于c列时选择b列否则选择c列

select(case when a>b then a else b end),(case when b>c then b else c end)
from t

posted @ 2012-07-09 17:53  不会代码的人  阅读(172)  评论(0编辑  收藏  举报