关于我的表格简短:我有2个表:content
,users
content
结构类似于:
id |标题| cpc |用户|活性
users is:
id |用户| monetos
提示 - 我想选择contet.*
= 1 (n)的content.active
行。拥有此数据,SELECT
users.monetos
WHERE users.id
= content.user
来自之前的查询。
现在,通过最大(n)步骤,我通过 content.cpc 值递减users.monetos
值
当* users.monetos = 0或小于0时,我想更新content
和SET active =' 0' *
通过语言,我想分享 users.monetos 金额到每个内容条目(每个 content.cpc )。并且 users.monetos 不再生成当前content
进入不活跃。并且每个 content.user 执行此操作
我此时所做的事情如下所示。我现在看起来很糟糕,但我已经不知道该怎么做了。依靠你们。谢谢。
$kak2 = array();
$rs16 = $connector->query("SELECT user FROM content WHERE active='1'");
while($rw16 = $connector->fetchArray($rs16))
{
$users_ids[] = $rw16['user'];
}
$user_info2 = $connector->fetchArray("SELECT monetos,id FROM users WHERE id IN (".implode(',',$users_ids).")");
while($user_info = $connector->fetchArray($user_info2))
{
$current_entry_info2 = $connector->query("SELECT cpc,id FROM content WHERE user='$user_info[id]' ORDER BY date DESC");
while ($current_entry_info = $connector->fetchArray($current_entry_info2))
{
$user_info['monetos']= $user_info['monetos'] - $current_entry_info['cpc'];
if($user_info['monetos'] = 0)
{
$updt = $connector->query("UPDATE content SET active='0' WHERE id='$current_entry_info[id]' LIMIT 1");
}
}
}
答案 0 :(得分:0)
我想我已经了解了你要做的事情,所以构建了一个小代码片段,我认为应该做你想要做的事情。
请注意,这是未经测试的代码
/** get the cost of all content for all users **/
$query = "select b.id, sum(a.cpc) as cpc 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 ) {
$menotos = $row['cpc'];
$query = "select id, cpc from content where user={$row['id']}"
/** cycle each users content **/
foreach($contents = $connector->fetchArray($query) as $content) {
$menotos -= $content['cpc'];
if($menotos <= 0) {
$disable[] = $content['id'];
}
}
if( isset($disable) ) {
$connector->query("update content set active=0 where id in(" . implode(',', $disable) . ")";
}
}