SQL__数据查询语句__分组查询

SQL__数据查询语句__排列__分组

1.Order by—按指定方法进行排序( order by 后面可接很多列名,也可以只有一个列名 )
  • 举例说明

    
    	------以下两个查询查询的是相同的内容(一个包含了having(),一个包含了all)-------------
    	select Title,Vacations=SUM(VacationHours)
    	from HumanResources.Employee
    	where Title in('Recruiter','Stocker','DesignEngnieer')
    	Group by all Title                                ------使用了all,就默认显示全部内容(包括空值)
    	having SUM(Vacationhours) is  not null                         
    	ORder by SUM(Vacationhours) desc
    	
    	select Title,Vacations=SUM(VacationHours)
    	from HumanResources.Employee
    	where Title in('Recruiter','Stocker','DesignEngnieer')
    	Group by Title                                    -------没用all,就默认删除了NUll的部分          
    	ORder by SUM(Vacationhours) desc
    	
    
2.desc 等价于 descending order 简称倒序排列
  • 例子如下
    
    	select [SalesOrderDetailID],[UnitPrice],[ModifiedDate],[SalesOrderID]
    	from Sales.SalesOrderDetail
    	where [UnitPrice]>2000 and [SalesOrderDetailID]<10
    	order by [UnitPrice] desc
    	
    
3.asc 等价于 ascending order 简称正序排列
  • 例子如下

    
    	select [SalesOrderDetailID],[UnitPrice],[ModifiedDate],[SalesOrderID]
    	from Sales.SalesOrderDetail
    	where [UnitPrice]>2000 and [SalesOrderDetailID]<10
    	order by 2 asc---因为[UnitPrice在select后是第二个列---
    	-----偷懒的方法:order by + 数字
    	
    	select [SalesOrderDetailID],[UnitPrice],[ModifiedDate],[SalesOrderID]
    	from Sales.SalesOrderDetail
    	where [UnitPrice]>2000 and [SalesOrderDetailID]<10
    	order by [UnitPrice],[SalesOrderDetailID] asc
    	
    
4.升降序排列之 desc – asc
  • 例子如下

    
    	select GroupName,DepartmentID,Name
    	from Humanresources.Department 
    	order by GroupName,DepartmentID desc 
    	
    	select GroupName,DepartmentID,Name
    	from Humanresources.Department 
    	order by GroupName,DepartmentID asc
    
    
5.补充

注意: 由于where()没有having()的一部分功能。所以,当出现聚合函数,要对其进行限定时,都用having(),也只能用having.

  • 结束语…………………………福利时间到………………………………

    大家同为程序员,在这里给大家真诚的送上福利。

    福利链接点击这里!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

男孩子小杨

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值