1. 程式人生 > >數據查詢基礎

數據查詢基礎

給定 pow percent 更新 條件表達式 格式 replace 它的 所有

  

復習:
--查詢:select * from 表名
--添加:insert [into] 表名(列名)values(值)

Default關鍵字
第一種:通過insert select 語句添加(將現有表中的數據添加到已存在的表中)
Insert into <表名>(列名)
Select <列值>
From<源表名>
第二種:通過select into語句添加(將現有表中的數據添加到新表:不創建新表)
Select(列名)
Into <新表名>
From <源表名>


第三種:通過Union關鍵字來多行插入數據
Insert into<表名>(列名)
Select <列的值>union
……………
Select <列的值>
--修改:update 表名 set 列名 = 更新值 where 更新條件
--刪除:delete from 表名 where 條件

使用select語句進行查詢
語法:select <列名/* >
From <表名>
Where <查詢條件表達式>
[Order by<排序的列名>[asc或desc]]

查全部的行和列
-- 查詢所有學生信息
select * from Students--Score\Course

--查詢學生的姓名和地址
select SName,SAddress from Students

--查詢“北京”的學生
select SName,SAddress from Students where SAddress = ‘北京海澱‘

--select * from Students where SAddress like ‘%北京%‘

select SName,SAddress,SEmail from Students where SSex = 1

查部分列
-- 查部分列
select SCode,SName,SAddress from Students where SAddress = ‘四川成都‘


select SCode,SName,SAddress from Students where SAddress <> ‘四川成都‘


列別名
AS關鍵字
--列的別名
select SName as 學生姓名,SAddress AS 家庭住址 from Students
--列的別名
select SName as 學生姓名,SAddress AS 家庭住址 from Students where SAddress <> ‘上海松江‘

使用“=”命名列
select SName +‘~‘+ SName AS 姓名 from Students

select 姓名 = SName +‘~‘+ SName from Students

查詢空行
select * from Students where SEmail is null
select * from Students where SEmail = ‘‘

--查詢常量值
select *,‘清華大學‘ as School from Students where SGrade = 1
select *,‘北京大學‘ as School from Students where SGrade = 2


--限制行數(限制固定行數)
select top 2 * from Students [where SName = ‘‘]
--按百分比返回行
select top 50 percent * from Students [where SName = ‘‘]

查詢單列排序
--Order by 升序
select StudentID,Score from Score Order by Score
select StudentID as 學生編號,(Score * 0.9 + 5) as 綜合成績
from Score
where (Score* 0.9 + 5) > 60
Order by Score

--降序排序
select StudentID,Score from Score Order by Score DESC
--查詢多列排序
select StudentID as 學生編號,CourseID as 課程編號,Score as 成績
from Score
where Score > 60
Order by CourseID,Score

小結
select * from Student where Address LIKE ‘%河北%‘ and Sex = ‘男‘

--連表查詢
-- Result連接Student表,通過Result表中的StudentNo連接Student表中的StudentNo
查詢成績表中的前5條數據,並顯示姓名和成績這兩列,成績按照降序排列
select top 5 StudentName,StudentResult from Result inner join Student a
on Result.StudentNo = a.StudentNo
Order by StudentResult DESC


Sql Server中的函數
--尋找一個指定的字符串在另一個字符串中的起始位置
select CHARINDEX(‘name‘,‘My is Tom age name‘)
--返回傳遞給它的字符串長度
select LEN(‘Java 課程‘)
--把傳遞給它的字符串轉換為大寫
select UPPER(‘java ‘)--JAVA
--清除字符左邊的空格
select LTRIM(‘ 智障 ‘)
--清除字符右邊的空格
select RTRIM(‘智障 = 智障 ‘)
--從字符串右邊返回指定數目的字符
select right(‘買買提, 吐爾松‘,7)
--替換一個字符串中的字符
select REPLACE(‘莫樂可切.楊可‘,‘可‘,‘蘭‘)
--在一個字符串中,刪除指定長度的字符,並在該位置插入一個新的字符串
select STUFF(‘ABCDE‘,2,3, ‘我的音樂我的世界‘)--從開始數個

--日期函數
select GETDATE() as Time
--將指定的數值添加到指定的日期部分後的日期
select DATEADD(WW,5,‘01/01/2009‘)--第一個參數是年月日的縮寫,第二個要累加的數字,第三個日期格式
--兩個日期之間的指定日期部分的間隔
select DATEDIFF(DD,‘2001/01/01‘,‘2002/01/01‘)
--日期中指定日期部分的整數形式
select DATEPART(DAY,‘2000/01/9‘)
--日期中指定日期部分的字符串形式
select DATENAME(DW,‘2000/01/03‘)


--數學函數
--返回從0 到1 之間的隨機float 值
select RAND()
--取數值表達式的絕對值
select ABS(-50)
--取大於或等於指定數值、表達式的最小整數
select CEILING(43.00000000000000000001)
--取小於或等於指定表達式的最大整數
select FLOOR(43.0000000000000000000009)
--取數值表達式的冪值
select POWER(2,10)--2,100一處錯誤.000000。
--將數值表達式四舍五入為指定精度
select ROUND(43.05,1)
--對於正數返回+1,對於負數返回-1,對於則返回
select SIGN(0)
--取浮點表達式的平方根
select SQRT(3847289)


--系統函數
select CONVERT(varchar(6),12345)
--返回當前用戶的名字
select CURRENT_USER
--返回用於指定表達式的字節數
select DATALENGTH(‘abcd 漢字‘)--漢字就是兩個
--返回當前用戶所登錄的計算機名字
select HOST_NAME()
--返回當前所登錄的用戶名稱
select SYSTEM_USER
--從給定的用戶ID返回用戶名
select USER_NAME(1)

數據查詢基礎