Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Select first 10 records in mysql

Tags:

php

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
like image 342
Rajesh Avatar asked Oct 16 '25 17:10

Rajesh


2 Answers

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.

like image 140
Pekka Avatar answered Oct 18 '25 05:10

Pekka


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.

like image 34
Alexey Romanov Avatar answered Oct 18 '25 07:10

Alexey Romanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!