views:

53

answers:

4

I can't seem to get the desired result.

  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 db 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)

+2  A: 

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

hollsk
those columns are varchar
st4ck0v3rfl0w
Most databases do implicit conversion between INT and string/VARCHAR
OMG Ponies
A: 

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?
Cthulhu
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
st4ck0v3rfl0w
+4  A: 
LIMIT 20, 0  

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

Did you mean:

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

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.

OMG Ponies
I am dumb. It's official. Thank you.
st4ck0v3rfl0w