【问题标题】:Is it possible to loop through the mysql table rows and check columns?是否可以遍历 mysql 表行并检查列?
【发布时间】:2017-02-23 00:26:58
【问题描述】:

我有一个填充了以下列的“矩阵”表。

matrix_id, user_id, position_1, position_2, position_3
   1          1        1982        2251       5841
   2          2        6204         0          0
   3          3          0          0          0
   4          4          0          0          0

我基本上想做以下事情。

  1. 查找 user_id 最低且位置为空的行。
  2. 在上面的示例中,这将是 user_id 2 和 position_2。
  3. 我用查询更新了行。
  4. 然后我继续下一个空仓。由于 user_id 2 仍然有一个空的 position_3,我再次使用查询更新该行。
  5. 由于该行已完成,我将转到下一个具有空位的最高 user_Id。在这种情况下,它是 user_id 3,然后是 user_id 4。

如果我知道 user_id 是什么,我知道我可以完成以上所有操作。但是假设在这种情况下,我不知道 user_ids 是什么。那么查询会是什么样子?

这是我目前所拥有的。

$find_user = $db->prepare("SELECT * FROM matrix WHERE user_id > :user_id");
$find_user->bindValue(':user_id', 0);
$find_user->execute();
$result_user = $find_user->fetchAll(PDO::FETCH_ASSOC);
if(count($result_user) > 0) {
  foreach($result_user as $row) {
    $matrix_id              = $row['matrix_id'];
    $user_id                = $row['user_id'];
    $position_1               = $row['position_1'];
    $position_2               = $row['position_2'];
    $position_3               = $row['position_3'];
  }
} else {
  $errors[] = 'User Id not found in Matrix.';
} 

$update_user = $db->prepare("UPDATE matrix SET position_2 = :position_2 WHERE user_id = :user_id");
$update_user->bindValue(':position_2', 1564;
$update_user->bindParam(':user_id', $user_id);
if($update_user->execute()) {}

【问题讨论】:

  • 您认为 EMPTY 是什么? ''NULL 或其他什么?
  • 我认为空为“0”。桌子就是这样。所有空位自动列为 0。我应该更新我的原始表格,以免造成混乱。
  • 所有新值都设置为1564
  • 最好的建议是修复这个损坏的设计
  • 不,所有新值都是唯一的。 “草莓”,请告知我该如何修复这个损坏的设计?

标签: php mysql database loops


【解决方案1】:

这应该遍历所有用户,从最小的 user_id 到最大。

对于每个用户,它会按顺序检查相关列并将新值应用于空列。

$new_val = 1999;

$result = $db->query("SELECT * FROM matrix ORDER BY user_id");
$users = $result->fetchAll(PDO::FETCH_ASSOC);
if(count($users) > 0) {

    // prepare all the possible queries
    // make use of prepare once execute many times
    $stmt1 = $db->prepare("UPDATE `matrix` SET `position_1` = :pos WHERE `user_id` = :id");
    $stmt2 = $db->prepare("UPDATE `matrix` SET `position_2` = :pos WHERE `user_id` = :id");
    $stmt3 = $db->prepare("UPDATE `matrix` SET `position_3` = :pos WHERE `user_id` = :id");

    foreach($users as $user) {
        if ( $user['$position_1'] == 0 ) {
            $stmt1->execute( array(':pos'=>++$new_val,':id'=>$user['user_id']) );
        }
        if ( $user['$position_2'] == 0 ) {
            $stmt1->execute( array(':pos'=>++$new_val,':id'=>$user['user_id']) );

        }
        if ( $user['$position_3'] == 0 ) {
            $stmt1->execute( array(':pos'=>++$new_val,':id'=>$user['user_id']) );
        }
    }
} else {
  $errors[] = 'User Id not found in Matrix.';
} 

您可以通过稍微更改查询来减少要处理的行数,以仅查找具有要修复的列的用户

$result = $db->query("SELECT * 
                    FROM matrix 
                    WHERE position_1 = 0 
                       OR position_2 = 0 
                       OR position_3 = 0 
                    ORDER BY user_id");

【讨论】:

  • 这很棒。我会用我的原始代码试一试。非常感谢。
  • 所以我在上面尝试了你的代码。我似乎收到错误“在 Matrix 中找不到用户 ID”。所以看起来 "$result->num_rows > 0" 没有返回任何记录。你确定“$result”查询是正确的吗?如果要查找行数,那么您不会在查询中使用“COUNT(*)”吗?让我知道。谢谢。
  • 您好,我的回答是基于您的问题。基本上这个位SELECT * FROM matrix WHERE user_id > :user_id 所以如果user_id 不存在它叫什么
  • 您好,这是我的完整代码的链接,可以为您提供更好的图片。我用你的方法做到了。我测试了它。它不只插入 1 个用户。如果有 20 个空位置,它将插入 20 个新用户并用这些新用户更新这 20 个位置。 paste.ofcode.org/xLF5qnmy2gPu73FHUKJQ7H如果您有任何问题,请告诉我。
  • 另外整个代码都是基于这个矩阵模型的。 pasteboard.co/BYTZF7nYw.jpg
【解决方案2】:

这里重要的是,您使用的是行,而不是列 所以检查所有先决条件并更新行。

$find_user = $db->prepare("SELECT * FROM matrix order by user_id asc");
$find_user->execute();
$result_user = $find_user->fetchAll(PDO::FETCH_ASSOC);
  foreach($result_user as $row) {
    $matrix_id  = $row['matrix_id'];
    $user_id    = $row['user_id'];
    $position_1 = $row['position_1'];
    $position_2 = $row['position_2'];
    $position_3 = $row['position_3'];
  }
 $str = ''
 if (!$position_2){
   $str = "position_2 = :position_2"
 } else if (!$position_2 && !$position_3){
   $str = "position_2 = :position_2 and position_3 = :position_3"
 }    


$update_user = $db->prepare("UPDATE matrix SET " . $str . " WHERE user_id = :user_id");
$update_user->bindValue(':position_2', 1564);
$update_user->bindValue(':position_3', 1564);
$update_user->bindParam(':user_id', $user_id);
if($update_user->execute()) {}

此外,获取矩阵表中按 used_id 排序的所有行,并根据您的条件处理每一行。

【讨论】:

  • 我认为asc 不是desc
  • $srt =/= $str
  • 空的position_1
  • thnx 伙计们,position_1 不在条件中
  • 我的问题,如果我在数百名用户下,每个用户有 3 个位置,在表中,这将如何工作?
猜你喜欢
  • 1970-01-01
  • 2015-03-10
  • 2014-03-06
  • 1970-01-01
  • 2012-06-23
  • 1970-01-01
  • 2020-11-25
  • 2021-10-23
  • 2018-11-22
相关资源
最近更新 更多