以上是我画的一个方案。有一个理想的情况,当 content.user
被分组。但通常他们不分组。
我在这个方案中的意思是:
第一步,我选择
users.monetos
哪里users.id = content.user
在第二步,我递减
users.monetos
与每个content.cpc
值(2.1,2.2)
模拟时:
选择content.user
(9)
选择users.monetos
哪里users.id=content.users
(15)
所以我们有 15 个值 users.monetos
对于 users.id=9
,现在我们回到内容表
和:
- 将 15 值减 8 (
content.cpc
) (15-8=7> 0 -> 转到步骤 2) - 将 7(上一步的结果)减 10 (
content.cpc
) (7-10=-3 <0 -> 更新 content set active='0' where content.id= (获得否定结果时的当前 id) )
对于每个 content.user
都是这样
更多扩展 - 我想选择contet.*
具有 content.active
的行= 1 (n)。有了这些数据,SELECT
users.monetos
哪里 users.id
=content.user
来自之前的查询。
现在,按最大 (n) 步数递减 users.monetos
按 content.cpc 值计算的值
当 **users.monetos=0 或小于 0 时,我想更新 content
并设置事件='0'**
言下之意,我想分享每个内容条目的users.monetos金额(每个content.cpc)。并且不再有 users.monetos 构成当前的 content
条目无效。对每个 content.user 执行此操作
我此时所做的如下所示。我现在看起来很糟糕,但我已经不知道该怎么办了。指望你们。谢谢。
$query = "select content.id, content.cpc, conent.user, content.active from content a
join users b on a.user=b.id
group by b.id where a.active='1'";
/** cycle each user **/
foreach($rows = $connector->fetchArray($query) as $row ) {
$monetos = $row['monetos'];
$query = "select id, cpc from content where user={$row['id']}";
/** cycle each users content **/
foreach($contents = $connector->fetchArray($query) as $content) {
echo $monetos;
$monetos -= $content['cpc'];
if($monetos <= 0) {
$disable[] = $content['id'];
}
}
if( isset($disable) ) {
$connector->query("update content set active='0' where id in(".implode(',',$disable).")");
}
}
最佳答案
通过使用 GROUP_CONCAT
,我们将 ID
和 CPC
进行分组,并用逗号分隔以供以后使用,并使用 GROUP BY
用户 ID
我们将获得每个用户的单行结果。
在 foreach
上,我们从 MONETOS
推导出每个 CPC
,并从那里我们将需要禁用的人员设置为 $ to_disable
数组,稍后用于禁用所有需要的 id。
$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 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;
关于php - 根据另一个表逐步更新一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22614306/