Contributed by BBQ
on May 01, 2000 at 11:25 UTC
Q&A
> database programming
Description: How does one go about selecting X number of rows on a query (e.g. with MySQL)? The desired effect would be to return results from 0-20, then 20-40, and so on.
Answer: How to display from X to Y records using DBI? contributed by btrott I'll expand on what chromatic wrote, because the
LIMIT keyword can do even more than just that. :)
In addition to specifying the max number
of records to return, you can specify an offset
of where to start in the records being
returned. The syntax is
[LIMIT [offset,] rows]
So, for example, to view records 10-35,
do this:
select log_id
from log_id
limit 10,25
Pretty neat! Consult the
MySQL SELECT docs
for more details and tips. | Answer: How to display from X to Y records using DBI? contributed by chromatic You could use the SQL command LIMIT:
SELECT * from presidents WHERE terms > 2 LIMIT 10;
You can also use the DBI method fetchall_arrayref(), which will return a reference to a list of rows. Use an array slice to get at the ones you want:
# prepare and execute statement
my $array_ref = $sth->fetchall_arrayref();
# we want 0 through 20
foreach my $row (@$array_ref[0 .. 20]) {
# do something
}
|
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|