Xavier_Ex
Xavier_Ex

Reputation: 8810

MySQL: selecting rows one batch at a time using PHP

What I try to do is that I have a table to keep user information (one row for each user), and I run a php script daily to fill in information I get from users. For one column say column A, if I find information I'll fill it in, otherwise I don't touch it so it remains NULL. The reason is to allow them to be updated in the next update when the information might possibly be available.

The problem is that I have too many rows to update, if I blindly SELECT all rows that's with column A as NULL then the result won't fit into memory. If I SELECT 5000 at a time, then in the next SELECT 5000 I could get the same rows that didn't get updated last time, which would be an infinite loop...

Does anyone have any idea of how to do this? I don't have ID columns so I can't just say SELECT WHERE ID > X... Is there a solution (either on the MySQL side or on the php side) without modifying the table?

Upvotes: 2

Views: 2590

Answers (1)

Raceimaztion
Raceimaztion

Reputation: 9644

You'll want to use the LIMIT and OFFSET keywords.

SELECT [stuff] LIMIT 5000 OFFSET 5000;

LIMIT indicates the number of rows to return, and OFFSET indicates how far along the table is read from.

Upvotes: 4

Related Questions