SQL server 常用语句

记录一些简单常用的sql语句

一、查询组

表中结构与数据

1、查询所有结果:select * from Test_Table

结果:

2、查询单独一列:select Test1 from Test_Table

3、按条件查询:select Test1 from Test_Table where ID=1

 4、插入一行数据:insert into Test_Table values (4,'test11','test12','test13')

                  

        执行结果                                                       查询结果

5、更新数据:update Test_Table set Test3='test14' where Test1='test11'

                       

              执行结果                                                                 查询结果

6、给列起别名:select ID AS '编号',Test1 as '第一列',Test2 as '第二列',Test3 as '第三列' from Test_Table

7、新添加一列:alter Table Test_Table add Test4 int                

                              注:最后int 为Test4的字段类型

     

                    执行结果                                                                       重新查询

8、批量循环更新数据:

declare @i int  
set @i=0
while @i<5
begin
    update Test_Table set Test4 = @i+5 
    set @i=@i +1
end

                    执行结果                                                                                查询结果

二、数据操作

表中的元数据为:

1、替换查询结果中的数据

select ID,Test4=
case  
     when Test4<60  then '未及格' 
     when Test4>=60 and Test4<90  then '合格'
     else '优秀' 
     end 
from Test_Table

 执行结果如下:

 2、求最大数:select max(Test4) from Test_Table

3、求最小数:select min(Test4) from Test_Table

4、求平均数:select avg(Test4) from Test_Table

5、求和:select sum(Test4) from Test_Table

6、统计满足条件的行数:select count(*) from Test_Table where Test4>60

狼的性格,羊的行为
原文地址:https://www.cnblogs.com/sunjianping/p/11163990.html