1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| SELECT prod_name FROM Products ORDER BY prod_name; -- 以 prod_name 列按字母顺序排序数据,ORDER BY 应为 SELECT 语句最后一条子句
SELECT prod_name, prod_price, prod_name FROM Products ORDER BY prod_price, prod_name; -- 先按 prod_price 排序,再按 prod_name 排序
SELECT prod_id, prod_price, prod_name FROM Products ORDER BY 2,3; -- 以上等价形式,优点无需重新输入列名,缺点可能用错列名排序,对 SELECT 进行更改容易忘记更改 ORDER BY 子句,进行排序列需在 SELECT 清单中
SELECT prod_id, prod_price FROM Products ORDER BY 2,prod_name; -- 混用两种形式
SELECT prod_id, prod_price, prod_name FROM Products ORDER BY prod_price DESC; -- 降序按 prod_price 排列数据
SELECT prod_id, prod_price, prod_name FROM Products ORDER BY prod_price DESC, prod_name; -- 先降序按 prod_price 排列,再正序按 prod_name 排列
SELECT prod_id, prod_price, prod_name FROM Products ORDER BY prod_price DESC, prod_name DESC; -- 在 prod_price 和 prod_name 同时按降序排序,每个都指定 DESC 关键字
SELECT prod_id, prod_price, prod_name FROM Products ORDER BY prod_price ASC, prod_name ASC; -- ASC 关键字按正序排列,无需单独声明
|