SQL Server(三):Select语句

 

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(
10over(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
原文地址:https://www.cnblogs.com/jingli6174/p/5522906.html