MySQL - count number of rows AFTER query
I currently have a query running on my site which looks for all rows which don't have a particular attribute. So although I have sequential numbers for the row numbers, the actual result will have gaps开发者_运维百科 in the primary keys. Turns out this is difficult to explain, so I'll try and add some code!
Here's the WHERE part of my statement, the rest is unimportant:
...WHERE
thought_deleted = 0
AND
thought_ID <= (SELECT MAX(thought_ID) FROM rmt_thoughts ORDER BY thought_ID) - $start
ORDER BY
thought_date_created DESC
LIMIT $number
$number and $start are values passed to the query using PHP. The part I want to change is the AND part. Instead of looking for thought_ID values which are less than a particular ID, I want to select as if each thought_deleted = 0 row has a sequential ID. I hope this makes sense!
I'm assuming I need to either a) make some kind of alias which counts the number of rows, and sort by that or b) just do some sort of limit thingy which only looks at the values I want. Either way, I've been googling for a while I am pretty exhausted and stuck. Has anyone seen anything like this before?
Thanks guys :)
Sparkles*
Apparently you want to do some kind of pagination.
Simply specify an offset using LIMIT <offset>, <limit>
instead of just LIMIT <limit>
.
For example, to retrieve rows 1..5 you use LIMIT 0, 5
and for the next 5 rows you use LIMIT 5, 5
.
In your case you probably want to use LIMIT $start, $number
assuming $start is zero-based.
精彩评论