0

I can't seem to get the desired result from this query:

  SELECT * 
    FROM `messages` 
   WHERE `msgType` = '0' 
     AND `status` = '0' 
ORDER BY `dateSent` DESC LIMIT 20, 0 

Basically, I'm trying to show 20 results per page, but this query returns nothing. (For the record, all instances in the database have msgType and status as 0)

EDIT: Removing the LIMIT part gives me the results but not divided and paginated like I want

EDIT v2 LIMIT should be followed by OFFSET, # OF RECORDS (I am dumb)

1
  • 1
    What flavor of SQL are you using? Commented Jul 23, 2010 at 19:05

4 Answers 4

5
LIMIT 20, 0  

Means: start at row 21, return 0 rows, so your answer is correct.

Did you mean:

LIMIT 0, 20  
Sign up to request clarification or add additional context in comments.

4 Comments

The offset is zero based, but that is the issue here
There are a total of 24 records. When I do LIMIT 1,20 it gives me records 23, 22, 21, 20....4
You are totally correct, a some collision in my brain for some reason, I'll edit accordingly..
@st4ck0v3rfl0w: You're not the first to get parameters backwards - I prefer questions like these to the denormalized data models...
2

Try removing the single quotes from around your 0's?

1 Comment

Most databases do implicit conversion between INT and string/VARCHAR
1

It took Cthulhu's answer to jog my memory - the issue is the LIMIT clause.

In MySQL, when LIMIT takes two parameters - the first is the offset, meaning which row it starts from, where the first row is zero. So:

LIMIT 20, 0

...will start on the 21st row, and return... zero rows from that point.

You need to reverse the values to get anything back:

LIMIT 0, 20

...to get the first 20 rows.

1 Comment

I am dumb. It's official. Thank you.
0

What results do you get when removing the limit? Simplify, even if the query is already simple to begin with. Break the problem up into parts.

  • How many rows are in the DB matching the conditions?
  • What is the datatype of msgType and status?
  • What happens when you remove the limit?

1 Comment

All rows match the conditions. Datatype of msgType and status is varchar. When I remove the limit, it provides the results in time sequence starting from the latest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.