標籤:
1、最基本的Select語句:
Select [Top n [With Ties]]
<*|Column_Name [As <Alias>][, ...n]>
From <Table_name>
Order by <Column_Name [DESC]>[, ...n]
1)*(星號)表示所有列,在選擇特定列時可以在結果集中更改顯示的列名
Select * from Products
Select ProductID,ProductName,CategoryID,UnitPrice
From Products
Select ProductID As ID,ProductName As Name,CategoryID,UnitPrice As Price
From Products
2)在結果集中可以使用運算式計算資料行
Select ProductID,ProductName,CategoryID,UnitPrice,
OutPrice=UnitPrice*1.2
From Products
3)Order by對結果集中的列進行排序,如果倒序,加DESC,如果是多列,選按第一列排序,如果第一列相同,按第二列排序,依此類推
Select ProductID,ProductName,CategoryID,UnitPrice
From Products
Order by CategoryID,Unitprice Desc
4)Top n:顯示結果集中的前n行,使用Top n時可以不存在Order by;Top n With Ties:如果第n行後存在與第n行相等的值,則也顯示這些行,使用Top n With Ties時,一定要有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時後接條件運算式,條件運算式可以是:
1)使用比較操作符串連的條件
2)使用邏輯操作符串連的條件
3)使用Between...and串連的條件:
where c betweeb v1 and v2相當於where c>=v1 and c<=v2
4)使用in:
where c in(v1,v2,v3)相當於where c=v1 or c=v2 or c=v3
5)使用Is Null或Is Not Null
6)使用like做字串的模糊查詢,其中支援的萬用字元有:
底線,表示任意單一字元;
星號,表示任意多個任一字元;
[<list>],表示單一字元,字元必須是列表中存在的字元;
[^<list>],表示單一字元,字元必須是列表中不存在的字元;
3、匯總和分類匯總
1)使用聚集合函式進行資料匯總,使用Group by <Column_Name [, ...n]>進行分類匯總
Select sum(UnitPrice) as [SUM]
From Products
Select CategoryID, sum(UnitPrice) as [SUM]
From Products
group by CategoryID
2)查詢的列必須是在Group By中出現的類
3)必須按條件陳述式(where)、分類匯總語句(group by)、排序語句(order by)的順序查詢。系統也將按照條件陳述式(where)、分類匯總語句(group by)、排序語句(order by)的順序執行。
Select CategoryID,sum(UnitPrice) as [SUM]
From Products
Where ProductID<50
group by CategoryID
Order By [Sum] Desc
4)如果對匯總結果實現條件,使用Having子句,不可以使用Where條件。
4、關於排名等的函數
在SQL Server中新引入的函數: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)內串連
(2)外串連
(3)交叉串連
6、子查詢
1)做為單值使用:要求查詢的結果為單行單列,與比較操作符搭配使用。
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)做為多值使用:要求查詢的結果為單列,與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)做為結果集(也可以簡單地理解為一個“表”)使用。
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
SQL Server(三):Select語句