mysql> select * from book2; +------+------+ | a | b | +------+------+ | 10 | a | | 8 | b | | 2 | c | | 16 | d | +------+------+ 4 rows in set (0.00 sec)
mysql> select * from book2 order by a desc; +------+------+ | a | b | +------+------+ | 16 | d | | 10 | a | | 8 | b | | 2 | c | +------+------+ 4 rows in set (0.00 sec)
mysql> select * from book2 order by a asc; +------+------+ | a | b | +------+------+ | 2 | c | | 8 | b | | 10 | a | | 16 | d | +------+------+ 4 rows in set (0.00 sec)
mysql> select * from book2 order by a; +------+------+ | a | b | +------+------+ | 2 | c | | 8 | b | | 10 | a | | 16 | d | +------+------+ 4 rows in set (0.00 sec)
select 列名 from 表名 limit 0,n; 或者 select 列名 from 表名 limit n;
例子6:下面的例子展示了,如何获取订单表中的前2行记录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
mysql> select t.id as 订单编号,t.price as 订单金额 from t_order as t limit 0,2; +--------------+--------------+ | 订单编号 | 订单金额 | +--------------+--------------+ | 1 | 88.95 | | 2 | 100.68 | +--------------+--------------+ 2 rows in set (0.00 sec)
mysql> select t.id as 订单编号,t.price as 订单金额 from t_order as t limit 2; +--------------+--------------+ | 订单编号 | 订单金额 | +--------------+--------------+ | 1 | 88.95 | | 2 | 100.68 | +--------------+--------------+ 2 rows in set (0.00 sec)
获取最大的一条记录
现在又有一个需求,需要获取订单金额中最大的一条记录,此时应该如何操作呢?
这里提供一个思路,先按照订单金额进行降序排列,然后取第一条记录即可。
例子7:下面的例子展示了,如何获取订单金额中最大的一条记录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mysql> select t.id as 订单编号,t.price as 订单金额 from t_order as t order by 订单金额 desc limit 0,1; +--------------+--------------+ | 订单编号 | 订单金额 | +--------------+--------------+ | 3 | 500.00 | +--------------+--------------+ 1 row in set (0.00 sec)
mysql> select t.id as 订单编号,t.price as 订单金额 from t_order as t order by 订单金额 desc limit 1; +--------------+--------------+ | 订单编号 | 订单金额 | +--------------+--------------+ | 3 | 500.00 | +--------------+--------------+ 1 row in set (0.00 sec)
mysql> select t.id as 订单编号,t.price as 订单金额 from t_order as t order by 订单金额 desc; +--------------+--------------+ | 订单编号 | 订单金额 | +--------------+--------------+ | 3 | 500.00 | | 4 | 300.00 | | 6 | 200.50 | | 2 | 100.68 | | 1 | 88.95 | | 5 | 20.88 | +--------------+--------------+ 6 rows in set (0.00 sec)
mysql> select t.id as 订单编号,t.price as 订单金额 from t_order as t order by 订单金额 desc limit 0,2; +--------------+--------------+ | 订单编号 | 订单金额 | +--------------+--------------+ | 3 | 500.00 | | 4 | 300.00 | +--------------+--------------+ 2 rows in set (0.00 sec)
mysql> select t.id as 订单编号,t.price as 订单金额 from t_order as t order by 订单金额 desc limit 2,2; +--------------+--------------+ | 订单编号 | 订单金额 | +--------------+--------------+ | 6 | 200.50 | | 2 | 100.68 | +--------------+--------------+ 2 rows in set (0.00 sec)
mysql> select t.id as 订单编号,t.price as 订单金额 from t_order as t order by 订单金额 desc limit 4,2; +--------------+--------------+ | 订单编号 | 订单金额 | +--------------+--------------+ | 1 | 88.95 | | 5 | 20.88 | +--------------+--------------+ 2 rows in set (0.00 sec)
limit使用中的一些坑
limit中不能使用表达式
参看如下代码:
1 2 3 4
mysql> select * from t_order limit 1,1+1; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '+1' at line 1 mysql> select * from t_order limit 1+1,1; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '+1,1' at line 1
也就是说limit字句中不允许使用表达式,后面必须是明确的数字。
limit后面2个参数不能为负数
参看如下代码:
1 2 3 4 5 6
mysql> select * from t_order limit -1; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1' at line 1 mysql> select * from t_order limit 0,-1; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1' at line 1 mysql> select * from t_order limit -1,-1; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1,-1' at line 1