Rajesh
Rajesh

Reputation: 163

How to Select first 10 records in mysql

In my database have 100 records but i want only first 10 records in descending order not for whole database in descending order.

Ex: Database:Records

 1,2,3,4,5,6,,7,8,9,10,11,12....................100.

First 10 Records:

10,9,8,7,6,5,4,3,2,1

Upvotes: 3

Views: 31414

Answers (5)

noblexenon
noblexenon

Reputation: 31

Use LIMIT. See the mySQL manual on SELECT

For example:

SELECT id FROM tablename ORDER BY ID LIMIT 0,10

The turning around of the results like you show is then probably best done in PHP using array_reverse(), I can't think of a easy MySQL way to do this.

Upvotes: 3

Nikki
Nikki

Reputation: 3318

You can use this query:

SELECT * FROM (SELECT * FROM table ORDER BY * ASC LIMIT 10) 
ORDER BY * DESC ;

Upvotes: 5

Alexey Romanov
Alexey Romanov

Reputation: 170919

If I understand your question correctly,

SELECT x FROM (SELECT x FROM table ORDER BY x ASC LIMIT 10) ORDER BY x DESC

The SELECT in parentheses selects the first 10 records (by ascending x) and the outer SELECT sorts them in the order you want.

Upvotes: 8

Pekka
Pekka

Reputation: 449823

Use LIMIT. See the mySQL manual on SELECT

For example:

SELECT id FROM tablename ORDER BY ID LIMIT 0,10

the turning around of the results like you show is then probably best done in PHP using array_reverse(), I can't think of a easy mySQL way to do this.

Upvotes: 7

robert
robert

Reputation: 34458

Use limit.

Upvotes: 0

Related Questions