在SQL中,LIMIT
函數的用法取決于你使用的數據庫管理系統(DBMS),例如MySQL、PostgreSQL、SQLite等。以下是幾種常見DBMS中LIMIT
函數的用法:
SELECT column1, column2, ...
FROM table_name
[ORDER BY column1, column2, ...]
LIMIT number_of_rows;
例如,要查詢employees
表的前10條記錄,你可以這樣寫:
SELECT * FROM employees
LIMIT 10;
SELECT column1, column2, ...
FROM table_name
[ORDER BY column1, column2, ...]
LIMIT number_of_rows OFFSET offset_value;
例如,要查詢employees
表的第11條到第20條記錄,你可以這樣寫:
SELECT * FROM employees
LIMIT 10 OFFSET 10;
SELECT TOP number_of_rows column1, column2, ...
FROM table_name
[ORDER BY column1, column2, ...];
例如,要查詢employees
表的前10條記錄,你可以這樣寫:
SELECT TOP 10 * FROM employees;
SELECT column1, column2, ...
FROM (SELECT column1, column2, ...
FROM table_name
[ORDER BY column1, column2, ...])
WHERE ROWNUM <= number_of_rows;
例如,要查詢employees
表的前10條記錄,你可以這樣寫:
SELECT * FROM (
SELECT * FROM employees
ORDER BY employee_id
)
WHERE ROWNUM <= 10;
請根據你使用的DBMS選擇合適的LIMIT
函數用法。