SQL ORDER BY学习W3Cschool

  • Post author:
  • Post category:其他




SQL ORDER BY 关键字


  • ORDER BY 关键字用于按升序或降序对结果集进行排序。


  • ORDER BY 关键字默认情况下按升序排序记录。


  • 如果需要按降序对记录进行排序,可以使用DESC关键字。



SQL ORDER BY 语法

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;


可以在ORDER BY子句中使用多个列,但要确保用于对该列进行排序的列应该在列表中


在这里插入图片描述


1.ORDER BY 实例



要求: SQL 语句从 “Customers” 表中选取所有客户,并按照 “Country” 列排序:

select * from customers order by Country;


2.ORDER BY DESC 实例



要求:SQL 语句从 “Customers” 表中选取所有客户,并按照 “Country” 列降序排序:

select * from customers order by Country desc;


3.ORDER BY 多列 实例



要求:SQL 语句从 “Customers” 表中选取所有客户,并按照 “Country” 和 “CustomerName” 列排序:

SELECT * FROM Customers 
ORDER BY Country, CustomerName; 


ORDER BY 多列 实例2



要求:SQL语句从”Customers” 表中选择所有客户,按 “Country” 升序排列,并按 “CustomerName” 列降序排列:

SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;



版权声明:本文为xiaolute原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。