简体   繁体   English

MySQL:使用PHP一次选择一行

[英]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. 我想做的是,我有一个表来保存用户信息(每个用户一行),并且每天运行一个php脚本来填写从用户那里获得的信息。 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. 对于A列,请说A列,如果我找到信息,我会填写它,否则我不会触摸它,因此它保持为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. 问题是我有太多行要更新,如果我盲目地将A列的所有行都选择为NULL,那么结果将不适合内存。 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... 如果我一次选择SELECT 5000,那么在下一个SELECT 5000中,我可以得到上次未更新的相同行,这将是一个无限循环...

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? 我没有ID列,所以我不能只说SELECT WHERE ID> X...。有没有不修改表的解决方案(在MySQL端还是在php端)?

You'll want to use the LIMIT and OFFSET keywords. 您将要使用LIMIT和OFFSET关键字。

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. LIMIT指示要返回的行数,而OFFSET指示从表中读取多远。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM