当前位置:Gxlcms > 数据库问题 > SQL Server(三):Select语句

SQL Server(三):Select语句

时间:2021-07-01 10:21:17 帮助过:2人阅读

 * 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

SQL Server(三):Select语句

标签:

人气教程排行