How to select the first 10 items from a database using SQL?

A type prompt appears SELECT * FROM clients ORDER BY id. I want to select only the first 10 items. How can i do this?

Postscript I am using MySQL.

+5
source share
5 answers
SELECT * FROM clients ORDER BY id LIMIT 10;
+12
source

Here you can only do SELECT (taken from here ):

SELECT
    [ALL | DISTINCT | DISTINCTROW]
      [HIGH_PRIORITY]
      [STRAIGHT_JOIN]
      [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
      [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr ...]
    [FROM table_references
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [PROCEDURE procedure_name(argument_list)]
    [INTO OUTFILE 'file_name' export_options
      | INTO DUMPFILE 'file_name'
      | INTO var_name [, var_name]]
    [FOR UPDATE | LOCK IN SHARE MODE]]

, , , :

SELECT * FROM clients ORDER BY id LIMIT 10
+4

, OFFSET :

LIMIT 10 OFFSET 11

10.

+3

:

SELECT TOP 10 * FROM clients ORDER BY id
+2

MySQL -

SELECT * FROM ORDER BY id LIMIT 10;

MySQL. , SQL: 2008 :

SELECT * FROM clients FETCH FIRST 10 ROWS ONLY;

and

SELECT * FROM clients OFFSET 1 FETCH NEXT 10 ROWS ONLY;

But the problem is that this syntax is not supported by MySQL and most other databases. If you care about portability, you should keep an eye on development there.

Keep in mind that you should always specify ORDER BY otherwise the result may be random with different calls.

0
source

All Articles