按组更新用户在数据库中的记录

时间:2013-11-28 21:52:19

标签: php mysql sql

我有这个表:tbl_module_bid

图片:

http://i.stack.imgur.com/4hQBN.png

你看到用户:Ali2,Ali,blackbone,dickface,mhmd 让我们调用它们中的每一个它叫做$ player和我想用的SQL查询:

mysql_query("UPDATE `bbcsystem`.`tbl_admin` SET games_played = games_played + 1 WHERE username = $player");

我尝试使用以下脚本:

//Update Game Played (not working very good):
    $num_qry        =   "Select DISTINCT *  From tbl_module_bid where  user = '".$_SESSION['LOGIN_BALANCE_FRONT']['name']."' AND module = '$mod_id'  order by bid asc";
    $get_pick       =   $db->get_results($num_qry,ARRAY_A);
                foreach($get_pick as $arr_pic)
                {
                    $player = $arr_pic['user'];
                    mysql_query("UPDATE `bbcsystem`.`tbl_admin` SET games_played = games_played + 1 WHERE username = $player");
                }

嗯,我想要做的是,取tbl_module_bid中的所有用户名,并为表中的每个用户,我将在另一个表中更新他的信息(Played) 我真的很新的stackoverflow和phpp .. 请考虑帮助我帮助我:) 感谢。

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

UPDATE tbl_admin a
INNER JOIN tbl_module_bid b on a.username = b.user
SET a.games_played = games_played + 1
WHERE b.user = $_SESSION['LOGIN_BALANCE_FRONT']['name']
      AND b.module = $mod_id

这可以在一个查询中完成您的两个查询。

答案 1 :(得分:0)

如果@Filipe说得对,那么这是一个干净的版本

<?php
$q = 'UPDATE tbl_admin AS a'
    .' INNER JOIN tbl_module_bid AS b ON a.username=b.user'
    .' SET a.games_played=games_played + 1 WHERE b.module = "'.$mod_id.'"';
$result = mysql_query($q);

不要忘记将答案标记为答案