如何最小化查询

时间:2011-02-01 16:19:07

标签: php mysql pdo

我有2个表,例如,需要4个元组升级或插入 表格将为每个玩家提供游戏和职业生涯的条目(2)所以这是4个元组。我只能想到为player1和游戏做一个SELECT如果不存在插入其他更新,那么对于player1和一个职业,.......这似乎很多数据库查询是否有一个更好的方法来处理这个?在PHP PDO中MYSQL现有代码:

CREATE TABLE `standings` (  
 `id` int(11) NOT NULL AUTO_INCREMENT,  
 `league_id` int(11) NOT NULL,  
 `season_id` int(11) NOT NULL,  
 `team_id` int(11) NOT NULL,  
 `statistic_type_id` int(11) NOT NULL,  
 `wlt` varchar(30) NOT NULL);    


$sth = $dbh->prepare('Select * from standings   
            where league_id=?  and season_id=? and team_id=? and statistic_type_id=$? ');  

$data = $sth->execute(array($l, $s, $t, $st));  

$data = $sth->fetchAll();  

if ($sth->rowCount() == 0) {    
    $wlt = $w . '," . $lo . ',' . $di;
    $sth = $dbh->prepare('INSERT INTO standings ( id, league_id, season_id,     
                team_id, statistic_type_id, wlt)  
                VALUES( 0, ?, ?, ?, ?, ?); 

         $data = $sth->execute(array( $l, $s, $t, $st, $wlt)); 

} else {  
    foreach ($data as $row) { 
        $wlt = explode(",", $row['wlt']);   
        $wlt[0] = $wlt[0] + $w;   
        $wlt[1] = $wlt[1] + $lo;    
        $wlt[2] = $wlt[2] + $di;   
        $nwlt = implode(",", $wlt);  

        $sth = $dbh->prepare('UPDATE standings SET wlt=?   
            where league_id=?  and season_id=? and team_id=? and statistic_type_id=$? ');  

        $data = $sth->execute(array($nwlt, $l, $s, $t, $st)); 
     }
} 

1 个答案:

答案 0 :(得分:0)

相关问题