記錄一些簡單經常使用的sql語句sql
1、查詢組
表中結構與數據spa
一、查詢全部結果:select * from Test_Table
結果:3d
二、查詢單獨一列:select Test1 from Test_Table
三、按條件查詢:select Test1 from Test_Table where ID=1
四、插入一行數據:insert into Test_Table values (4,'test11','test12','test13')
執行結果 查詢結果code
五、更新數據:update Test_Table set Test3='test14' where Test1='test11'
執行結果 查詢結果blog
六、給列起別名:select ID AS '編號',Test1 as '第一列',Test2 as '第二列',Test3 as '第三列' from Test_Table
七、新添加一列:alter Table Test_Table add Test4 int
注:最後int 爲Test4的字段類型class
執行結果 從新查詢test
八、批量循環更新數據:
declare @i int set @i=0 while @i<5 begin update Test_Table set Test4 = @i+5 set @i=@i +1 end
執行結果 查詢結果date
2、數據操做
表中的元數據爲:select
一、替換查詢結果中的數據
select ID,Test4= case when Test4<60 then '未及格' when Test4>=60 and Test4<90 then '合格' else '優秀' end from Test_Table
執行結果以下:sql語句