Computing.Net > Forums > Programming > MySQL Query Statement

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

MySQL Query Statement

Reply to Message Icon

Name: Clicker
Date: November 17, 2004 at 11:29:27 Pacific
OS: Win XP
CPU/Ram: 1.6 G / 512 MB
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: November 17, 2004 at 11:51:38 Pacific
Reply:

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.



0

Response Number 2
Name: FishMonger
Date: November 18, 2004 at 08:57:43 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


very basic thing batch file help



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: MySQL Query Statement

PHP and MYSQL, query wont work www.computing.net/answers/programming/php-and-mysql-query-wont-work/10069.html

php code to email mysql query to me www.computing.net/answers/programming/php-code-to-email-mysql-query-to-me/10499.html

MySQL queries www.computing.net/answers/programming/mysql-queries/6318.html