0

I've attempted to search but not sure I'm getting the keywords correct for this.

I want to select rows in desc order that aren't the last 5 rows but the 5 before that!

So if I had 140 rows, I want rows 131-135.

The following ets me 136-140.

SELECT id, title, content, date FROM tbl_news ORDER BY id DESC LIMIT 5

How could I change the limit value to achieve 131-135?

Thanks.

5 Answers 5

3

You want to use the two parameter limit:

SELECT id, title, content, date FROM tbl_news ORDER BY id DESC LIMIT 5, 5

This will give you the first 5 rows after row 5(so rows 6, 7, 8, 9, 10) which is the 5 rows before the last 5 like you want.

Edit: Fiddle here: http://sqlfiddle.com/#!2/9113a/1

It should return id's 4-8 as there are 12 records here.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help, figured that would let you mess around the query to understand how it works.
0

Limit can take 2 parameters, offset (which is zero based) and quantity, so you can use this:

SELECT id, title, content, date FROM tbl_news ORDER BY id DESC LIMIT 10, 5

To get 5 rows, starting at row 10

For you case, you can do

SELECT id, title, content, date FROM tbl_news ORDER BY id ASC LIMIT 129, 5

1 Comment

The figures I quoted were just to help understand the question. Are rows will grow and I always want the 5 previous to the last 5. Will your first query do this?
0
SELECT id, title, content, date FROM tbl_news ORDER BY id DESC LIMIT 5,5;

Comments

-1
SELECT id, title, content, date FROM tbl_news ORDER BY id DESC LIMIT 10,5

2 Comments

yes, the first parameter is the offset the second is the amount to take. So it would offset by 10 then take 5
Except the OP wanted an offset by 5, which you dont explain in your post what the values represent.
-1

Does this work for you?

select top 5 id
from tbl_news
where id not in
(
    select top 5 id
    from tbl_news
    order by id desc
)
order by id desc

4 Comments

You might be talking about other database systems. top is not recognized in MySql.
Was going to say the same! Never come across top using mysql before?
That's true...I had assumed MySql had top function
All database systems are very different. mssql for example doesnt have the LIMIT command. You need to know what system you are working on before providing an answer because of all of these differences in key words.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.