简体   繁体   English

选择限制并更新表格。 限制不能正常工作。 sql

[英]select with limit and update table. Limit not working fine. sql

I am trying to update thable with select using limit 我正在尝试使用select使用限制来更新thable
It's update table fine when we enter "limit" only with one parameter in select query like (limit 50) 当我们在选择查询中仅使用一个参数(limit 50)输入“限制”时,更新表就可以了

But when select with "limit" like ( limit $sqlFrom, $sqlTo ) it updates the table but skip 2nd (51 to 100) records and again start updating from 101. 但是,当使用“ limit”(例如limit $sqlFrom, $sqlTo )进行选择时,它将更新表,但跳过第二条记录(51至100),并再次从101开始更新。

It skip every 2nd 50 records 每2个50记录跳过一次

Where is the problem??? 问题出在哪儿???

Here is the code 这是代码

$sqlFrom  = 0;
$sqlTo    = 50;

for($try = 0; $try < 6; $try++) 
{   

 $res = mysql_query("select * from table_name where underprocess = 0 limit " . $sqlFrom . "," . $sqlTo);

 while($row = mysql_fetch_array($res))
 {
     $id = $row['id'];

    mysql_query("update table_name set underprocess = 1 where id = " . $id) or die('error');
    echo $id;

 }


print '<hr/>';
if($sqlFrom != 0)
{
    $sqlFrom += $sqlTo;
}
else
{
    $sqlFrom = $sqlTo;
}
}//for

This is as expected 这是预期的

When you update rows 1-50 based on your first iteration, you set underprocess = 1 . 当基于第一次迭代更新行1-50时,将设置underprocess = 1 These are ignored in the 2nd call because ask for rows underprocess = 0 . 在第二次调用中将忽略它们,因为要求行underprocess = 0

So rows 51-100 in the 1st query are now rows 1-50 for your 2nd query if you like. 因此,如果愿意,第一个查询中的第51-100行现在为第二个查询中的第1-50行。

You don't need to change your LIMIT range because you always want the "first" 50. 您不需要更改LIMIT范围,因为您始终想要“第一个” 50。

NOTE 注意

Your LIMIT isn't guaranteed anyway because you have no ORDER BY. 无论如何,由于您没有ORDER BY,因此无法保证您的LIMIT。

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

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