Dùng mệnh đề LIMIT để giới hạn số hàng trả về.
Ví dụ
Bảng ratelist có 5 hàng:
mysql> Select * from ratelist ORDER BY Price; +----+------+-------+ | Sr | Item | Price | +----+------+-------+ | 5 | T | 250 | | 1 | A | 502 | | 2 | B | 630 | | 4 | h | 850 | | 3 | C | 1005 | +----+------+-------+ 5 rows in set (0.00 sec)
Chỉ lấy 3 hàng đầu:
mysql> Select * from ratelist ORDER BY Price LIMIT 3; +----+------+-------+ | Sr | Item | Price | +----+------+-------+ | 5 | T | 250 | | 1 | A | 502 | | 2 | B | 630 | +----+------+-------+ 3 rows in set (0.00 sec)
Cú pháp mở rộng: LIMIT offset, count (vd: LIMIT 2, 3 lấy 3 hàng bắt đầu từ hàng thứ 3 - offset 0-based).