Jump to content

[SOLVED] MySQL query sucks or my programming sucks?


himegoto

Recommended Posts

SELECT * FROM blog ORDER BY id DESC LIMIT 7

 

I made a simple blog and I wanted it to display the first 2 rows from the above query, then put the next 5 into another section.  That part works fine.  Then you click one of those 5, it kicks up the id # from the link and goes something this, where 3 is the id# and 7 is the max # of records I want to pull up:

 

SELECT * FROM blog ORDER BY id DESC LIMIT 3,7

 

This is where the problem is.  I expect to get the first row as id #3, then the next is id# 2, then id #1.  But thats not whats happening.

Instead, with the above example.  I get id #6, 5, 4, then 3.  If I change the 3 to 1, I get id 8 down to 2 and 1 never even comes up.

You can easily see what I'm talking about at http://tremor.myftp.biz/srcg/index.php?mode=blog&id=3.

 

So what sucks more? My novice MySQL skills or my novice PhP skills?

 

 

Link to comment
Share on other sites

using "Limit a,b" is mainly for pagination(that is, if you have too much to display on one page, you make page numbers and each page # is calculated and the proper value placed in front of the comma). You're getting (a) number of records starting from the (b)th record. So in your example, you're getting the first 7 rows, starting with the 3rd row.

Link to comment
Share on other sites

Thats what I want, but not what I'm getting.  Another example is I want 7 rows starting at id 13 so I use

SELECT * FROM blog ORDER BY id DESC LIMIT 13, 7

What I expect to get is first row id # 13, then 7 more down to id #6.  but what I get instead is id #3, #2, and #1. Nothing more.

In my understanding, that query should never even come close to calling up those 3 ids.  That is what leads me to believe it something with my query and not my coding.  But if the query is correct, that makes me even more confused so I guess I'll try the php forum.

Link to comment
Share on other sites

"13" in the LIMIT clause has nothing to to do with record id 13.

 

First it gets the results of your query then puts them in order as specified in the ORDER BY.

 

so if you have 20 records you get

 

ID

---

20

19

18

.

.

.

4

3

2

1

 

It will then return 7 (or as many as remain) records starting at offset 13 (14th row) in these results, which should be

 

7

6

5

4

3

2

1

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.