根据另一个表逐步更新表

时间:2014-03-24 15:54:29

标签: php mysql

enter image description here

以上是我画的一个方案。当content.user被分组时,有一个理想的情况。但通常他们没有分组。 我在这个计划中的意思是:

  1. 在第一步,我选择users.monetos WHERE users.id = content.user

  2. 在第二步,我使用users.monetos值(2.1,2.2)递减content.cpc

  3. 模拟时:  选择content.user(9)

    选择users.monetos其中users.id=content.users(15)

    因此users.monetos的{​​{1}}值为15,现在我们回到内容表 并且:

    1. 使用8(users.id=9)递减15值(15-8 = 7 > 0 - >转到第2步)
    2. 递减7(上一步的结果),其中10(content.cpc)(7-10 = -3< 0->更新 content set active ='0'其中content.id =(获得否定结果时的当前id))
    3. 对于每个content.cpc

      都是如此

      更多扩展 - 我想选择content.user = 1 (n)contet.*行。拥有此数据,content.active SELECT WHERE users.monetos = users.id来自之前的查询。 现在,通过最大(n)步骤,我通过 content.cpc 值递减content.user当** users.monetos = 0或小于0时​​,我想更新users.monetos并且SET active ='0'**

      通过语言,我想分享 users.monetos 金额到每个内容条目(每个 content.cpc )。并且没有更多 users.monetos 制作当前content 进入不活跃。并且每个 content.user 执行此操作 我此时所做的事情如下所示。我现在看起来很糟糕,但我已经不知道该怎么做了。依靠你们。谢谢。

      content

2 个答案:

答案 0 :(得分:1)

使用GROUP_CONCAT我们将IDCPC分隔开以逗号分隔以供日后使用,并GROUP BY用户ID我们将获得每个用户一行结果。

foreach上,我们从CPC推断出每个MONETOS,然后从那里我们设置需要禁用的$to_disable数组,后来用于禁用所有需要的是。

$query = "SELECT b.id AS user_id,
                 b.monetos,
                 GROUP_CONCAT(a.id ORDER BY a.id DESC) AS content_ids, 
                 GROUP_CONCAT(a.cpc ORDER BY a.id DESC) AS cpc,
            FROM content a
            JOIN users b 
              ON a.user = b.id
        GROUP BY b.id";

$to_disable = array();
$to_enable = array();
foreach($rows = $connector->fetchArray($query) as $row)
{
    $monetos = $row['monetos'];
    $data = array_combine(explode(',',$row['content_ids']), explode(',',$row['cpc']));
    echo "USER {$row['user_id']} currently have {$monetos}!<br>\n";
    foreach ($data as $content_id => $cpc)
    {
        $monetos -= $cpc;
        echo "USER {$row['user_id']} after CONTENT {$content_id} now have {$monetos}!<br>\n";
        if ($monetos <= 0)
        {
            echo "USER {$row['user_id']} should have the CONTENT {$content_id} disabled!<br>\n";
            $to_disable[] = $content_id;
        }
        else
        {
            echo "USER {$row['user_id']} should have the CONTENT {$content_id} enabled!<br>\n";
            $to_enable[] = $content_id;
        }
    }
    echo "<br>\n";
}

if (sizeof($to_disable) > 0)
{
    $connector->query("UPDATE content 
                          SET active = 0
                        WHERE id IN (".implode(',',$to_disable).")");
}
echo "UPDATE content SET active = 0 WHERE id IN (".implode(',',$to_disable).")<br>\n";

if (sizeof($to_enable) > 0)
{
    $connector->query("UPDATE content 
                          SET active = 1
                        WHERE id IN (".implode(',',$to_enable).")");
}
echo "UPDATE content SET active = 0 WHERE id IN (".implode(',',$to_enable).")";

使用您的SQL转储,这是我得到的:

USER 9 currently have 15!
USER 9 after CONTENT 16 now have 10!
USER 9 after CONTENT 30 now have 5!
USER 9 after CONTENT 17 now have 4!
USER 9 after CONTENT 31 now have -1!
USER 9 should have the CONTENT 31 disabled!
USER 9 after CONTENT 18 now have -4!
USER 9 should have the CONTENT 18 disabled!
USER 9 after CONTENT 32 now have -9!
USER 9 should have the CONTENT 32 disabled!
USER 9 after CONTENT 20 now have -13!
USER 9 should have the CONTENT 20 disabled!
USER 9 after CONTENT 33 now have -18!
USER 9 should have the CONTENT 33 disabled!
USER 9 after CONTENT 21 now have -22!
USER 9 should have the CONTENT 21 disabled!
USER 9 after CONTENT 34 now have -26!
USER 9 should have the CONTENT 34 disabled!
USER 9 after CONTENT 22 now have -31!
USER 9 should have the CONTENT 22 disabled!
USER 9 after CONTENT 24 now have -36!
USER 9 should have the CONTENT 24 disabled!
USER 9 after CONTENT 26 now have -41!
USER 9 should have the CONTENT 26 disabled!
USER 9 after CONTENT 29 now have -45!
USER 9 should have the CONTENT 29 disabled!

USER 10 after CONTENT 28 now have 95!

USER 11 after CONTENT 27 now have -4!
USER 11 should have the CONTENT 27 disabled!

UPDATE结果:

UPDATE content SET active = 0 WHERE id IN (31,18,32,20,33,21,34,22,24,26,29,27)

以下是用于读取数据的示例代码:

<?php    
// Your database info
$db_host = '';
$db_user = '';
$db_pass = '';
$db_name = '';

$con = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "SELECT b.id AS user_id,
               b.monetos,
               GROUP_CONCAT(a.id ORDER BY a.id DESC) AS content_ids, 
               GROUP_CONCAT(a.cpc ORDER BY a.id DESC) AS cpc
          FROM content a
          JOIN users b 
            ON a.user = b.id
      GROUP BY b.id";
$result = $con->prepare($sql);
$result->execute();

if ($result->rowCount() == 0)
{
    die('No data found...');
}

$to_disable = array();
$to_enable = array();
foreach($result->fetchALL(PDO::FETCH_ASSOC) as $row)
{
    $monetos = $row['monetos'];
    $data = array_combine(explode(',',$row['content_ids']), explode(',',$row['cpc']));
    echo "USER {$row['user_id']} currently have {$monetos}!<br>\n";
    foreach ($data as $content_id => $cpc)
    {
        $monetos -= $cpc;
        echo "USER {$row['user_id']} after CONTENT {$content_id} now have {$monetos}!<br>\n";
        if ($monetos <= 0)
        {
            echo "USER {$row['user_id']} should have the CONTENT {$content_id} disabled!<br>\n";
            $to_disable[] = $content_id;
        }
        else
        {
            echo "USER {$row['user_id']} should have the CONTENT {$content_id} enabled!<br>\n";
            $to_enable[] = $content_id;
        }
    }
    echo "<br>\n";
}

if (sizeof($to_disable) > 0)
{
    $ids = implode(',',$to_disable);
    $sql = "UPDATE content 
               SET active = 0
             WHERE id IN ({$ids})";
    $disable = $con->prepare($sql);
    $disable->execute();
    echo "UPDATE content SET active = 0 WHERE id IN ({$ids})<br>\n";
}
else
{
    echo "Nothing was disabled...<br>\n";
}

if (sizeof($to_enable) > 0)
{
    $ids = implode(',',$to_enable);
    $sql = "UPDATE content 
               SET active = 1
             WHERE id IN ({$ids})";
    $enable = $con->prepare($sql);
    $enable->execute();
    echo "UPDATE content SET active = 1 WHERE id IN ({$ids})";
}
else
{
    echo "Nothing was enabled...";
}
$con = NULL;

答案 1 :(得分:1)

你能尝试两个UPDATE语句吗?

UPDATE
  users u JOIN
  ( SELECT user, SUM(cpc) AS cpc FROM content GROUP BY user) as c ON (u.id=c.user)
SET u.monetos = u.monetos - c.cpc;
UPDATE content AS c
  SET c.active = 0
WHERE
  (SELECT u.monetos FROM users u WHERE c.user = u.id) <= 0;

如果允许users.monetos变为否定,我没有得到。如果没有,您可以将IF()检查添加到SET u.monetos

SQL Fiddle