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
source to share
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.
source to share
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
source to share
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.
source to share