記錄一些簡單常用的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
執行結果如下: