select * from student limit 0,5
select * from student order by id asc limit 5
前五条记录的查询语句。
查询后5条,注意结果为倒序排序,要使用desc
select * from student order by id desc limit 5
select * from student limit m,n
返回m+1到m+n行记录,m代表开始的下标,n代表查找的结果数,将返回n行结果
select * from student limit 2,8返回3到10行记录
使用模拟的 ROW_NUMBER + PARTITION 的方式SELECT
sale_item,
sale_date,
sale_money
FROM
(
SELECT
CASE
WHEN @cn != sale_item THEN @rownum:= 1
ELSE @rownum:= @rownum + 1
END AS No,
@cn := sale_item AS sale_item,
sale_date,
sale_money
FROM
(SELECT @rownum:=0) r,
(SELECT @cn:='') p,
SALE_REPORT
ORDER BY
SALE_ITEM,
SALE_DATE DESC
) subQuery
WHERE
no <=3
+-----------+---------------------+------------+
| sale_item | sale_date | sale_money |
+-----------+---------------------+------------+
| A | 2012-12-31 00:00:00 | 2012.00 |
| A | 2012-12-30 00:00:00 | 2012.00 |
| A | 2012-12-29 00:00:00 | 2012.00 |
| B | 2012-12-31 00:00:00 | 12.00 |
| B | 2012-12-30 00:00:00 | 12.00 |
| B | 2012-12-29 00:00:00 | 12.00 |
| C | 2012-12-31 00:00:00 | 31.00 |
| C | 2012-12-30 00:00:00 | 30.00 |
| C | 2012-12-29 00:00:00 | 29.00 |
+-----------+---------------------+------------+
9 rows in set (0.02 sec)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)