Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a question about a MySQL query.
I want to grab 10 rows at one time. I know about setting up a nested for loop and doing multiple queries and store them in an array and keep repeating. But is there a way to query multiple rows in one single SQL command.Thanks
Clicker

Not sure I understand your question.
Let's say that you have a table that lists the lastname of the governor of each state in the US. The table has two fields, lastname and state. The state field contains two character codes for the states.
select * from gov where state = "NE";
This will return one row.
select * from gov;
This will return all 50 rows. I did a single query and got back multiple rows. Now to process these rows, I have to use a loop because I can process only one row at a time. Exactly how you do that depends upon the language. But typically, you execute the query, then enter a loop that fetches the next row, then processes it. The loop repeats until there are no more rows to fetch.
So, I've queried "...multiple rows in one single SQL command".
I wonder if this is what you meant tho. You say that you "...want to grab 10 rows at one time". Using the 'fetch' mechanism of the language, you can process ten rows, if the query returns at least ten rows. You could easily make the code stop after ten rows, even if more were returned.
But there is not a way to get all ten rows returned by one fetch type statement.

You need to use the LIMIT clause to select 10 rows at a time.
select * FROM table LIMIT 10;
However, if you're doing this within a loop, you'll be "reselecting" the same rows each time through the loop. So, you'll also need to tell it the starting point/row.
$start = 0; # initalized outside of the loop
select * FROM table LIMIT $start, 10;
$start += 10;It gets a liitle more complicated if you need to skip rows i.e., querying a subset of the table. That could be accomidated for with the use of an additional "yes/no" field that is modified after the rows are selected.

![]() |
very basic thing
|
batch file help
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |