码迷,mamicode.com
首页 > 其他好文 > 详细

ORDER_BY子句

时间:2020-05-27 23:20:59      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:rom   esc   选择   anti   tom   fir   point   根据   and   

ORDER BY常见的语法

  • ORDER BY 根据XX列进行排序, 在SELECT, WHERE, GROUP BY后面
  • LIMIT 一般放在最后, 与ORDER BY一起使用, 主要是对选择数量的限制
# ORDER BY
SELECT *
FROM customers
ORDER BY state DESC, first_name;

SELECT first_name, last_name
FROM customers
ORDER BY birth_date;

SELECT first_name, last_name, 10 AS points
FROM customers
ORDER BY points, first_name;
-- 尽量避免使用1, 2
SELECT first_name, last_name, 10 AS points
FROM customers
ORDER BY 1, 2;

-- Exercise
-- Get the order_items order_id=2, and total_price desc
SELECT *, quantity * unit_price AS total_price
FROM order_items
WHERE order_id = 2
ORDER BY (quantity * unit_price) DESC;

SELECT *, quantity * unit_price AS total_price
FROM order_items
WHERE order_id = 2
ORDER BY total_price DESC;

# LIMIT
SELECT *
FROM customers
LIMIT 3;
-- page 1: 1 - 3
-- page 2: 4 - 6
-- page 3: 7 - 9
# 前面是偏移量, 从XX开始查询3条数据
SELECT *
FROM customers
LIMIT 6, 3;
-- Exercise
-- GET the top three loyal customers
SELECT *
FROM customers
ORDER BY points DESC
LIMIT 3;

ORDER_BY子句

标签:rom   esc   选择   anti   tom   fir   point   根据   and   

原文地址:https://www.cnblogs.com/jly1/p/12977289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!