SQL after more than

I am trying to sort the SQL results

id | name (table name: student)
-----
1 | jhon
2 | amin
3 | heli
4 | mir
5 | mrs
6 | amr
7 | jonnathan
8 | adhy

      

When I use this query

select id from studenth where id>='3' order by id DESC limit 2

      

The result is

id | name (table name: student)
-----
8 | adhy
7 | jonnathan

      

While I want to sort results after id = 3, I need below data

id | name (table name: student)
-----
4 | mir
3 | heli

      

+3


source to share


7 replies


select * from (select id from student where id >= 3 order by id limit 2) r order by r.id desc

      



+1


source


You can do the following:

select * from student where id>=3 order by id LIMIT 2

      

If you want to take the records and sort them in descending order, you can go to the subquery.

SELECT * FROM
( SELECT * 
  FROM student 
  WHERE id>=3 ORDER BY id LIMIT 2) X
ORDER BY X.id DESC

      



Problem with your request

select * from student where id>='3' order by id DESC limit 2

      

The above query will take all results with id=3

or more and order it in descending order, while when LIMIT 2

it only displays 2 records.

This is why it displays the last 2 entries.

0


source


Remove single quotes in this id>='3'

select id from student where id>=3 order by id desc limit 2

      

Update 1:

Order by desc limit 2 problem here, use subquery, get the result then apply downstream.

select * from (select id from student where id >= 3 order by id limit 2) new order by new.id desc

      

0


source


You can use this query. You want everything less than or equal to 4, not just after 3.

select id from student where id<='4' order by id DESC limit 2

      

0


source


If I understood correctly, you need data in descending order from the id +1 entry. the request should look something like this:

select id from studenth where id<='4' order by id DESC limit 2;

      

0


source


TRY THIS Use a helper query that's easy to understand and fulfill your requirement:

SELECT id, name 
FROM studenth 
WHERE id IN (SELECT id 
             FROM studenth 
             WHERE id >= 3 ORDER BY id LIMIT 2) ORDER BY id DESC

      

0


source


Please try the following ...

SELECT id,
       name
FROM tblTable
WHERE id >= 3
ORDER BY id DESC
LIMIT ( ( SELECT COUNT( * )
          FROM tblTable
          WHERE id >= 3 ) - 1 ), 2;

      

This operator works by sorting records from tblTable

based on value id

. It then uses it LIMIT

to select specific entries from the sorted list. If there are 20

eligibnle records , that means LIMIT 19, 2

it will give us the 19th and 20th records.

If you have any questions or comments, please do not hesitate to post a comment.

0


source







All Articles