1、最基本的Select語句:
Select [Top n [With Ties]]
<*|Column_Name [As ][, ...n]>
From
Order by[, ...n]
1)*(星號(hào))表示所有列,在選擇特定列時(shí)可以在結(jié)果集中更改顯示的列名
Select * from Products
Select ProductID,ProductName,CategoryID,UnitPrice
From Products
Select ProductID As ID,ProductName As Name,CategoryID,UnitPrice As Price
From Products
2)在結(jié)果集中可以使用表達(dá)式計(jì)算列
Select ProductID,ProductName,CategoryID,UnitPrice,
OutPrice=UnitPrice*1.2
From Products
3)Order by對(duì)結(jié)果集中的列進(jìn)行排序,如果倒序,加DESC,如果是多列,選按第一列排序,如果第一列相同,按第二列排序,依此類推
Select ProductID,ProductName,CategoryID,UnitPrice
From Products
Order by CategoryID,Unitprice Desc
4)Top n:顯示結(jié)果集中的前n行,使用Top n時(shí)可以不存在Order by;Top n With Ties:如果第n行后存在與第n行相等的值,則也顯示這些行,使用Top n With Ties時(shí),一定要有Order by。
Select Top 12
ProductID,ProductName,CategoryID,UnitPrice
From Products
Select Top 12 With Ties
ProductID,ProductName,CategoryID,UnitPrice
From Products
Order By UnitPrice
2、where條件子句:
使用where時(shí)后接條件表達(dá)式,條件表達(dá)式可以是:
1)使用比較操作符連接的條件
2)使用邏輯操作符連接的條件
3)使用Between...and連接的條件:
where c betweeb v1 and v2相當(dāng)于where c>=v1 and c<=v2
4)使用in:
where c in(v1,v2,v3)相當(dāng)于where c=v1 or c=v2 or c=v3
5)使用Is Null或Is Not Null
6)使用like做字符串的模糊查詢,其中支持的通配符有:
下劃線,表示任意單一字符;
星號(hào),表示任意多個(gè)任意字符;
[
Select [Top n [With Ties]]
<*|Column_Name [As ][, ...n]>
From
Order by
1)*(星號(hào))表示所有列,在選擇特定列時(shí)可以在結(jié)果集中更改顯示的列名
Select * from Products
Select ProductID,ProductName,CategoryID,UnitPrice
From Products
Select ProductID As ID,ProductName As Name,CategoryID,UnitPrice As Price
From Products
2)在結(jié)果集中可以使用表達(dá)式計(jì)算列
Select ProductID,ProductName,CategoryID,UnitPrice,
OutPrice=UnitPrice*1.2
From Products
3)Order by對(duì)結(jié)果集中的列進(jìn)行排序,如果倒序,加DESC,如果是多列,選按第一列排序,如果第一列相同,按第二列排序,依此類推
Select ProductID,ProductName,CategoryID,UnitPrice
From Products
Order by CategoryID,Unitprice Desc
4)Top n:顯示結(jié)果集中的前n行,使用Top n時(shí)可以不存在Order by;Top n With Ties:如果第n行后存在與第n行相等的值,則也顯示這些行,使用Top n With Ties時(shí),一定要有Order by。
Select Top 12
ProductID,ProductName,CategoryID,UnitPrice
From Products
Select Top 12 With Ties
ProductID,ProductName,CategoryID,UnitPrice
From Products
Order By UnitPrice
2、where條件子句:
使用where時(shí)后接條件表達(dá)式,條件表達(dá)式可以是:
1)使用比較操作符連接的條件
2)使用邏輯操作符連接的條件
3)使用Between...and連接的條件:
where c betweeb v1 and v2相當(dāng)于where c>=v1 and c<=v2
4)使用in:
where c in(v1,v2,v3)相當(dāng)于where c=v1 or c=v2 or c=v3
5)使用Is Null或Is Not Null
6)使用like做字符串的模糊查詢,其中支持的通配符有:
下劃線,表示任意單一字符;
星號(hào),表示任意多個(gè)任意字符;
[
- ],表示單一字符,字符必須是列表中存在的字符;
[^
- ],表示單一字符,字符必須是列表中不存在的字符;
3、匯總和分類匯總
1)使用聚集函數(shù)進(jìn)行數(shù)據(jù)匯總,使用Group by
Select sum(UnitPrice) as [SUM]
From Products
Select CategoryID, sum(UnitPrice) as [SUM]
From Products
group by CategoryID
2)查詢的列必須是在Group By中出現(xiàn)的類
3)必須按條件語句(where)、分類匯總語句(group by)、排序語句(order by)的順序查詢。系統(tǒng)也將按照條件語句(where)、分類匯總語句(group by)、排序語句(order by)的順序執(zhí)行。
Select CategoryID,sum(UnitPrice) as [SUM]
From Products
Where ProductID<50
group by CategoryID
Order By [Sum] Desc
4)如果對(duì)匯總結(jié)果實(shí)現(xiàn)條件,使用Having子句,不可以使用Where條件。
4、關(guān)于排名等的函數(shù)
在SQL Server中新引入的函數(shù):Rank、Dense_Rank、Row_Number、NTile(n)
Select ProductID,ProductName,UnitPrice,
Rank() over(Order By UnitPrice) as [Rank],
Dense_Rank() over(Order By UnitPrice) as [Dense_Rank],
Row_Number() over(Order By UnitPrice) as [Row_Number],
NTile(10) over(Order By UnitPrice) as [NTile]
From Products
5、多表連接
1)使用Where連接的情況
Select ProductID,ProductName,CategoryName
From Products,Categories
where Products.CategoryID=Categories.CategoryID
2)使用Join語句連接
Select ProductID,ProductName,CategoryName
From Products p join Categories c
on p.CategoryID=c.CategoryID
3)Join連接類型:
(1)內(nèi)連接
(2)外連接
(3)交叉連接
6、子查詢
1)做為單值使用:要求查詢的結(jié)果為單行單列,與比較操作符搭配使用。
declare @sum money
select @sum=sum(UnitPrice) from Products
select * from Products
where UnitPrice>@sum
Select * from
Where UnitPrice>(Select sum(UnitPrice) from Products)
2)做為多值使用:要求查詢的結(jié)果為單列,與In操作符搭配使用。
Select p.* from
Products p join Categories c on p.CategoryID=c.CategoryID
where CategoryName like ’c%’
Select * from Products
where CategoryID in
(Select CategoryID from Categories
where CategoryName like ’c%’)
3)做為結(jié)果集(也可以簡(jiǎn)單地理解為一個(gè)“表”)使用。
Select ProductID,ProductName,UnitPrice
from
(
Select ProductID,ProductName,UnitPrice
Row_Number() over(order by UnitPrice) as RowNumber
From Prodcuts
) as t
where RowNumber between 41 and 50

