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.
-
结束语…………………………福利时间到………………………………
大家同为程序员,在这里给大家真诚的送上福利。