第九章 資料查詢基礎
阿新 • • 發佈:2019-01-30
查詢
/****** Script for SelectTopNRows command from SSMS ******/ SELECT TOP 1000 [StudentNo] ,[LoginPwd] ,[StudentName] ,[Sex] ,[GradeId] ,[Phone] ,[Address] ,[BornDate] ,[Email] FROM [MySchool].[dbo].[Student] --查詢所有的資料行和列 SELECT * FROM dbo.Student 查詢部分行或列 SELECT StudentNo ,StudentName ,GradeId FROM dbo.Student WHERE Sex='女' AND GradeId=1 --查詢中使用列的別名 SELECT StudentNo AS 學號,StudentName AS 姓名, GradeId AS 年級 FROM dbo.Student WHERE Sex='男' AND GradeId=2 --查詢空值(採用 "IS NULL"或"IS NOT NULL"來判斷是否為空) SELECT 姓名=StudentName,性別=Sex,年級=GradeId,Email FROM dbo.Student WHERE Email IS NULL -------------- SELECT 姓名=StudentName,性別=Sex,年級=GradeId,Email FROM dbo.Student WHERE Email IS NOT NULL --查詢中使用常量列(將常量的預設值新增到查詢輸出中) SELECT '北大青鳥' AS 學校,StudentNo AS 學號,StudentName AS 姓名,Sex AS 性別,GradeId AS 年級,Phone AS 聯絡方式, Email FROM dbo.Student -- 查詢返回限制的行數 (查詢部分前十列,使用TOP關鍵字約束) SELECT TOP 10 StudentNo,StudentName,Sex,GradeId FROM dbo.Student WHERE GradeId=1 --查詢返回限制的行數 (查詢百分比提取記錄,使用PERCENT關鍵字來限制) SELECT TOP 20 PERCENT StudentNo AS 學號,StudentName AS 姓名,Sex AS 性別,GradeId AS 年級 FROM dbo.Student WHERE Sex='男' --(1990年後出生的學生姓名) SELECT * FROM dbo.Student WHERE BornDate>1990 -------(字串函式) --查詢所在位置 SELECT StudentNo AS 學號,StudentName AS 姓名, GradeId AS 年級,Phone AS 聯絡方式 ,CHARINDEX('6',Phone,1)as 位置,Address AS 地址 FROM dbo.Student WHERE Sex='男' AND GradeId=2 --查詢長度 SELECT StudentNo AS 學號,StudentName AS 姓名, GradeId AS 年級,Phone AS 聯絡方式 ,Address AS 地址,LEN(Address)AS 長度 FROM dbo.Student WHERE Sex='男' AND GradeId=2 --轉換成大寫 SELECT StudentNo AS 學號,StudentName AS 姓名, GradeId AS 年級,Phone AS 聯絡方式 ,Address AS 地址,Email,UPPER(Email)AS 大寫Email FROM dbo.Student WHERE Sex='男' AND GradeId=2 --清除字元左邊的空格 --新增空格 SELECT StudentNo AS 學號,' '+StudentName+' ' AS 姓名, GradeId AS 年級,Phone AS 聯絡方式 ,Address AS 地址,Email FROM dbo.Student WHERE Sex='男' AND GradeId=2 --左邊的空格 SELECT StudentNo AS 學號,' '+StudentName+' ' AS 姓名,LTRIM(StudentName), GradeId AS 年級,Phone AS 聯絡方式 ,Address AS 地址,Email FROM dbo.Student WHERE Sex='男' AND GradeId=2 --右邊的空格 SELECT StudentNo AS 學號,' '+StudentName+' ' AS 姓名,RTRIM(StudentName), GradeId AS 年級,Phone AS 聯絡方式 ,Address AS 地址,Email FROM dbo.Student WHERE Sex='男' AND GradeId=2 --從字串右邊返回指定數目的字元 SELECT StudentNo AS 學號,StudentName AS 姓名, GradeId AS 年級,Phone AS 聯絡方式 ,RIGHT(Phone,4)AS 右邊四位數, Address AS 地址,Email FROM dbo.Student WHERE Sex='男' AND GradeId=2 --從字串左邊返回指定數目的字元 SELECT StudentNo AS 學號,StudentName AS 姓名, GradeId AS 年級,Phone AS 聯絡方式 ,left(Phone,4)AS 左邊四位數, Address AS 地址,Email FROM dbo.Student WHERE Sex='男' AND GradeId=2 --替換一個字串中的字元 SELECT StudentNo AS 學號,StudentName AS 姓名, GradeId AS 年級,Phone AS 聯絡方式 , Address AS 地址,REPLACE(Address,'學生','優秀')AS 替換後地址, Email FROM dbo.Student WHERE Sex='男' AND GradeId=2 --刪除指定長度的字元,並在該位置插入新的字串 SELECT StudentName AS 姓名,Phone AS 聯絡方式,STUFF(Phone,3,5,'***')AS 插入後,Address AS 地址 FROM dbo.Student -------(日期函式) --yy,yyyy(年) mm,m(月) wk,ww(周) dw,w(日期) mi,n(分) ss,s(秒) --取得當前系統日期 SELECT StudentNo,StudentName,Address,BornDate,GETDATE()AS 當前日期,Email FROM dbo.Student --將指定的數值新增到指定日期(月份) SELECT StudentNo,StudentName,Address,BornDate,DATEADD(MM,4,BornDate )AS 新增後日期,Email FROM dbo.Student --指定日期部分的間隔(月份) SELECT StudentNo,StudentName,Address,BornDate,DATEDIFF(MM,'1995-9-9',BornDate)AS 日期間隔,Email FROM dbo.Student --指定日期部分字串形式(weekday) SELECT StudentNo,StudentName,Address,BornDate,DATENAME(DW,BornDate)AS 字串形式,Email FROM dbo.Student --指定日期部分的整數形式 --(整數--年) SELECT StudentNo,StudentName,Address,BornDate,DATEPART(year,BornDate)AS 整數型式,Email FROM dbo.Student --(整數--月) SELECT StudentNo,StudentName,Address,BornDate,DATEPART(month,BornDate)AS 整數型式,Email FROM dbo.Student --(整數--日) SELECT StudentNo,StudentName,Address,BornDate,DATEPART(DAY,BornDate)AS 整數型式,Email FROM dbo.Student --------數學函式 --返回從0到1之間的隨機float值 SELECT RAND() --取數值表示式的絕對值 SELECT ABS(-78),ABS(-63) --取整數 冪值 指定精度 平方根 SELECT CEILING(55.6), POWER(5,2), ROUND(45.321,1),ROUND(23.212,2),SQRT(9) --正數返回+1,負數返回-1,0則返回0 SELECT SIGN(-45),SIGN(23) ------系統函式 --轉變資料型別 返回當前使用者姓名 位元組數 計算機名字 所登入使用者名稱 使用者ID返回使用者名稱 SELECT CONVERT(VARCHAR(5),12345), CURRENT_USER , DATALENGTH('滴滴答答'),HOST_NAME(), SYSTEM_USER, USER_NAME(1) -- (E-maill中@的位置) SELECT Email,CHARINDEX('@',Email,1 ) FROM dbo.Student --(E-maill的長度) SELECT Email,LEN(Email) FROM dbo.Student
排序
/****** Script for SelectTopNRows command from SSMS ******/ SELECT TOP 1000 [Id] ,[StudentNo] ,[SubjectId] ,[StudentResult] ,[ExamDate] FROM [MySchool].[dbo].[Result] --檢視第一學期所有學生資訊 SELECT * FROM [MySchool].[dbo].[Result] WHERE SubjectId=1 --檢視第二學期所有學生的資訊 SELECT * FROM [MySchool].[dbo].[Result] WHERE SubjectId=2 --檢視第三學期所有學生的資訊 SELECT * FROM dbo.Result WHERE SubjectId=3 --檢視StudentResult超過60的資訊 SELECT * FROM dbo.Result WHERE StudentResult>60 --查詢排序(使用ORDER BY進行排序 ASC:"升序--[預設]" DESC:"降序") --ExamDate(升序輸出) SELECT * FROM dbo.Result ORDER BY ExamDate --StudentResult(降序輸出) SELECT * FROM dbo.Result ORDER BY StudentResult DESC --按日期,成績,編號為1的考試科目資訊 SELECT ExamDate,StudentResult,SubjectId FROM dbo.Result WHERE SubjectId=1 ORDER BY ExamDate , StudentResult DESC --前五名學生成績 SELECT TOP 5 StudentResult FROM dbo.Result WHERE ExamDate='2013-3-22' --查詢學號"S1101003"考試資訊,先後順序顯示 SELECT StudentNo,StudentResult,ExamDate FROM dbo.Result WHERE StudentNo='s1101003' ORDER BY ExamDate